blob: 5aa1082f0506346361e1fd16ba92070becede44e [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 Krejci00b874b2019-02-12 10:54:50 +010063#define COMPILE_MEMBER_ARRAY_GOTO(CTX, MEMBER_P, ARRAY_C, OPTIONS, FUNC, RET, GOTO) \
64 if (MEMBER_P) { \
65 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, 1, RET, GOTO); \
66 size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
67 LY_ARRAY_INCREMENT(ARRAY_C); \
68 RET = FUNC(CTX, MEMBER_P, OPTIONS, &(ARRAY_C)[__array_offset]); \
69 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
70 }
71
Radek Krejcid05cbd92018-12-05 14:26:40 +010072#define COMPILE_CHECK_UNIQUENESS(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \
73 if (ARRAY) { \
Radek Krejci0af46292019-01-11 16:02:31 +010074 for (unsigned int u__ = 0; u__ < LY_ARRAY_SIZE(ARRAY); ++u__) { \
75 if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__].MEMBER) == (void*)(IDENT)) { \
Radek Krejcid05cbd92018-12-05 14:26:40 +010076 LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \
77 return LY_EVALID; \
78 } \
79 } \
80 }
81
Radek Krejci19a96102018-11-15 13:38:09 +010082static struct lysc_ext_instance *
83lysc_ext_instance_dup(struct ly_ctx *ctx, struct lysc_ext_instance *orig)
84{
85 /* TODO */
86 (void) ctx;
87 (void) orig;
88 return NULL;
89}
90
91static struct lysc_pattern*
92lysc_pattern_dup(struct lysc_pattern *orig)
93{
94 ++orig->refcount;
95 return orig;
96}
97
98static struct lysc_pattern**
99lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig)
100{
Radek Krejcid05cbd92018-12-05 14:26:40 +0100101 struct lysc_pattern **dup = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100102 unsigned int u;
103
104 LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_SIZE(orig), NULL);
105 LY_ARRAY_FOR(orig, u) {
106 dup[u] = lysc_pattern_dup(orig[u]);
107 LY_ARRAY_INCREMENT(dup);
108 }
109 return dup;
110}
111
112struct lysc_range*
113lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig)
114{
115 struct lysc_range *dup;
116 LY_ERR ret;
117
118 dup = calloc(1, sizeof *dup);
119 LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL);
120 if (orig->parts) {
121 LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_SIZE(orig->parts), ret, cleanup);
122 LY_ARRAY_SIZE(dup->parts) = LY_ARRAY_SIZE(orig->parts);
123 memcpy(dup->parts, orig->parts, LY_ARRAY_SIZE(dup->parts) * sizeof *dup->parts);
124 }
125 DUP_STRING(ctx, orig->eapptag, dup->eapptag);
126 DUP_STRING(ctx, orig->emsg, dup->emsg);
127 dup->exts = lysc_ext_instance_dup(ctx, orig->exts);
128
129 return dup;
130cleanup:
131 free(dup);
132 (void) ret; /* set but not used due to the return type */
133 return NULL;
134}
135
136struct iff_stack {
137 int size;
138 int index; /* first empty item */
139 uint8_t *stack;
140};
141
142static LY_ERR
143iff_stack_push(struct iff_stack *stack, uint8_t value)
144{
145 if (stack->index == stack->size) {
146 stack->size += 4;
147 stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack);
148 LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM);
149 }
150 stack->stack[stack->index++] = value;
151 return LY_SUCCESS;
152}
153
154static uint8_t
155iff_stack_pop(struct iff_stack *stack)
156{
157 stack->index--;
158 return stack->stack[stack->index];
159}
160
161static void
162iff_stack_clean(struct iff_stack *stack)
163{
164 stack->size = 0;
165 free(stack->stack);
166}
167
168static void
169iff_setop(uint8_t *list, uint8_t op, int pos)
170{
171 uint8_t *item;
172 uint8_t mask = 3;
173
174 assert(pos >= 0);
175 assert(op <= 3); /* max 2 bits */
176
177 item = &list[pos / 4];
178 mask = mask << 2 * (pos % 4);
179 *item = (*item) & ~mask;
180 *item = (*item) | (op << 2 * (pos % 4));
181}
182
183#define LYS_IFF_LP 0x04 /* ( */
184#define LYS_IFF_RP 0x08 /* ) */
185
Radek Krejci0af46292019-01-11 16:02:31 +0100186/**
187 * @brief Find a feature of the given name and referenced in the given module.
188 *
189 * If the compiled schema is available (the schema is implemented), the feature from the compiled schema is
190 * returned. Otherwise, the special array of pre-compiled features is used to search for the feature. Such
191 * features are always disabled (feature from not implemented schema cannot be enabled), but in case the schema
192 * will be made implemented in future (no matter if implicitly via augmenting/deviating it or explicitly via
193 * ly_ctx_module_implement()), the compilation of these feature structure is finished, but the pointers
194 * assigned till that time will be still valid.
195 *
196 * @param[in] mod Module where the feature was referenced (used to resolve prefix of the feature).
197 * @param[in] name Name of the feature including possible prefix.
198 * @param[in] len Length of the string representing the feature identifier in the name variable (mandatory!).
199 * @return Pointer to the feature structure if found, NULL otherwise.
200 */
Radek Krejci19a96102018-11-15 13:38:09 +0100201static struct lysc_feature *
Radek Krejci0af46292019-01-11 16:02:31 +0100202lys_feature_find(struct lys_module *mod, const char *name, size_t len)
Radek Krejci19a96102018-11-15 13:38:09 +0100203{
204 size_t i;
Radek Krejci0af46292019-01-11 16:02:31 +0100205 struct lysc_feature *f, *flist;
Radek Krejci19a96102018-11-15 13:38:09 +0100206
207 for (i = 0; i < len; ++i) {
208 if (name[i] == ':') {
209 /* we have a prefixed feature */
Radek Krejci0af46292019-01-11 16:02:31 +0100210 mod = lys_module_find_prefix(mod, name, i);
Radek Krejci19a96102018-11-15 13:38:09 +0100211 LY_CHECK_RET(!mod, NULL);
212
213 name = &name[i + 1];
214 len = len - i - 1;
215 }
216 }
217
218 /* we have the correct module, get the feature */
Radek Krejci0af46292019-01-11 16:02:31 +0100219 if (mod->implemented) {
220 /* module is implemented so there is already the compiled schema */
221 flist = mod->compiled->features;
222 } else {
223 flist = mod->off_features;
224 }
225 LY_ARRAY_FOR(flist, i) {
226 f = &flist[i];
Radek Krejci19a96102018-11-15 13:38:09 +0100227 if (!strncmp(f->name, name, len) && f->name[len] == '\0') {
228 return f;
229 }
230 }
231
232 return NULL;
233}
234
235static LY_ERR
236lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, int UNUSED(options), struct lysc_ext_instance *ext)
237{
238 const char *name;
239 unsigned int u;
240 const struct lys_module *mod;
241 struct lysp_ext *edef = NULL;
242
243 DUP_STRING(ctx->ctx, ext_p->argument, ext->argument);
244 ext->insubstmt = ext_p->insubstmt;
245 ext->insubstmt_index = ext_p->insubstmt_index;
246
247 /* get module where the extension definition should be placed */
248 for (u = 0; ext_p->name[u] != ':'; ++u);
Radek Krejcie86bf772018-12-14 11:39:53 +0100249 mod = lys_module_find_prefix(ctx->mod_def, ext_p->name, u);
Radek Krejci19a96102018-11-15 13:38:09 +0100250 LY_CHECK_ERR_RET(!mod, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
251 "Invalid prefix \"%.*s\" used for extension instance identifier.", u, ext_p->name),
252 LY_EVALID);
253 LY_CHECK_ERR_RET(!mod->parsed->extensions,
254 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
255 "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100256 ext_p->name, mod->name),
Radek Krejci19a96102018-11-15 13:38:09 +0100257 LY_EVALID);
258 name = &ext_p->name[u + 1];
259 /* find the extension definition there */
260 for (ext = NULL, u = 0; u < LY_ARRAY_SIZE(mod->parsed->extensions); ++u) {
261 if (!strcmp(name, mod->parsed->extensions[u].name)) {
262 edef = &mod->parsed->extensions[u];
263 break;
264 }
265 }
266 LY_CHECK_ERR_RET(!edef, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
267 "Extension definition of extension instance \"%s\" not found.", ext_p->name),
268 LY_EVALID);
269 /* TODO plugins */
270
271 return LY_SUCCESS;
272}
273
274static LY_ERR
275lys_compile_iffeature(struct lysc_ctx *ctx, const char **value, int UNUSED(options), struct lysc_iffeature *iff)
276{
277 const char *c = *value;
278 int r, rc = EXIT_FAILURE;
279 int i, j, last_not, checkversion = 0;
280 unsigned int f_size = 0, expr_size = 0, f_exp = 1;
281 uint8_t op;
282 struct iff_stack stack = {0, 0, NULL};
283 struct lysc_feature *f;
284
285 assert(c);
286
287 /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */
288 for (i = j = last_not = 0; c[i]; i++) {
289 if (c[i] == '(') {
290 j++;
291 checkversion = 1;
292 continue;
293 } else if (c[i] == ')') {
294 j--;
295 continue;
296 } else if (isspace(c[i])) {
297 checkversion = 1;
298 continue;
299 }
300
301 if (!strncmp(&c[i], "not", r = 3) || !strncmp(&c[i], "and", r = 3) || !strncmp(&c[i], "or", r = 2)) {
302 if (c[i + r] == '\0') {
303 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
304 "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value);
305 return LY_EVALID;
306 } else if (!isspace(c[i + r])) {
307 /* feature name starting with the not/and/or */
308 last_not = 0;
309 f_size++;
310 } else if (c[i] == 'n') { /* not operation */
311 if (last_not) {
312 /* double not */
313 expr_size = expr_size - 2;
314 last_not = 0;
315 } else {
316 last_not = 1;
317 }
318 } else { /* and, or */
319 f_exp++;
320 /* not a not operation */
321 last_not = 0;
322 }
323 i += r;
324 } else {
325 f_size++;
326 last_not = 0;
327 }
328 expr_size++;
329
330 while (!isspace(c[i])) {
331 if (!c[i] || c[i] == ')') {
332 i--;
333 break;
334 }
335 i++;
336 }
337 }
338 if (j || f_exp != f_size) {
339 /* not matching count of ( and ) */
340 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
341 "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value);
342 return LY_EVALID;
343 }
344
345 if (checkversion || expr_size > 1) {
346 /* check that we have 1.1 module */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100347 if (ctx->mod_def->version != LYS_VERSION_1_1) {
Radek Krejci19a96102018-11-15 13:38:09 +0100348 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
349 "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value);
350 return LY_EVALID;
351 }
352 }
353
354 /* allocate the memory */
355 LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM);
356 iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr);
357 stack.stack = malloc(expr_size * sizeof *stack.stack);
358 LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx), error);
359
360 stack.size = expr_size;
361 f_size--; expr_size--; /* used as indexes from now */
362
363 for (i--; i >= 0; i--) {
364 if (c[i] == ')') {
365 /* push it on stack */
366 iff_stack_push(&stack, LYS_IFF_RP);
367 continue;
368 } else if (c[i] == '(') {
369 /* pop from the stack into result all operators until ) */
370 while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) {
371 iff_setop(iff->expr, op, expr_size--);
372 }
373 continue;
374 } else if (isspace(c[i])) {
375 continue;
376 }
377
378 /* end of operator or operand -> find beginning and get what is it */
379 j = i + 1;
380 while (i >= 0 && !isspace(c[i]) && c[i] != '(') {
381 i--;
382 }
383 i++; /* go back by one step */
384
385 if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) {
386 if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) {
387 /* double not */
388 iff_stack_pop(&stack);
389 } else {
390 /* not has the highest priority, so do not pop from the stack
391 * as in case of AND and OR */
392 iff_stack_push(&stack, LYS_IFF_NOT);
393 }
394 } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) {
395 /* as for OR - pop from the stack all operators with the same or higher
396 * priority and store them to the result, then push the AND to the stack */
397 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) {
398 op = iff_stack_pop(&stack);
399 iff_setop(iff->expr, op, expr_size--);
400 }
401 iff_stack_push(&stack, LYS_IFF_AND);
402 } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) {
403 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) {
404 op = iff_stack_pop(&stack);
405 iff_setop(iff->expr, op, expr_size--);
406 }
407 iff_stack_push(&stack, LYS_IFF_OR);
408 } else {
409 /* feature name, length is j - i */
410
411 /* add it to the expression */
412 iff_setop(iff->expr, LYS_IFF_F, expr_size--);
413
414 /* now get the link to the feature definition */
Radek Krejci0af46292019-01-11 16:02:31 +0100415 f = lys_feature_find(ctx->mod_def, &c[i], j - i);
416 LY_CHECK_ERR_GOTO(!f, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
417 "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]);
418 rc = LY_EVALID, error)
Radek Krejci19a96102018-11-15 13:38:09 +0100419 iff->features[f_size] = f;
420 LY_ARRAY_INCREMENT(iff->features);
421 f_size--;
422 }
423 }
424 while (stack.index) {
425 op = iff_stack_pop(&stack);
426 iff_setop(iff->expr, op, expr_size--);
427 }
428
429 if (++expr_size || ++f_size) {
430 /* not all expected operators and operands found */
431 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
432 "Invalid value \"%s\" of if-feature - processing error.", *value);
433 rc = LY_EINT;
434 } else {
435 rc = LY_SUCCESS;
436 }
437
438error:
439 /* cleanup */
440 iff_stack_clean(&stack);
441
442 return rc;
443}
444
Radek Krejci00b874b2019-02-12 10:54:50 +0100445static struct lysc_node *
446lys_compile_xpath_context(struct lysc_node *start)
447{
448 for (; start && !(start->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_ACTION | LYS_NOTIF));
449 start = start->parent);
450 return start;
451}
452
Radek Krejci19a96102018-11-15 13:38:09 +0100453static LY_ERR
Radek Krejci00b874b2019-02-12 10:54:50 +0100454lys_compile_when(struct lysc_ctx *ctx, struct lysp_when *when_p, int options, struct lysc_when **when)
Radek Krejci19a96102018-11-15 13:38:09 +0100455{
456 unsigned int u;
457 LY_ERR ret = LY_SUCCESS;
458
Radek Krejci00b874b2019-02-12 10:54:50 +0100459 *when = calloc(1, sizeof **when);
460 (*when)->refcount = 1;
461 (*when)->cond = lyxp_expr_parse(ctx->ctx, when_p->cond);
462 DUP_STRING(ctx->ctx, when_p->dsc, (*when)->dsc);
463 DUP_STRING(ctx->ctx, when_p->ref, (*when)->ref);
464 LY_CHECK_ERR_GOTO(!(*when)->cond, ret = ly_errcode(ctx->ctx), done);
465 COMPILE_ARRAY_GOTO(ctx, when_p->exts, (*when)->exts, options, u, lys_compile_ext, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100466
467done:
468 return ret;
469}
470
471static LY_ERR
472lys_compile_must(struct lysc_ctx *ctx, struct lysp_restr *must_p, int options, struct lysc_must *must)
473{
474 unsigned int u;
475 LY_ERR ret = LY_SUCCESS;
476
477 must->cond = lyxp_expr_parse(ctx->ctx, must_p->arg);
478 LY_CHECK_ERR_GOTO(!must->cond, ret = ly_errcode(ctx->ctx), done);
479
480 DUP_STRING(ctx->ctx, must_p->eapptag, must->eapptag);
481 DUP_STRING(ctx->ctx, must_p->emsg, must->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +0100482 DUP_STRING(ctx->ctx, must_p->dsc, must->dsc);
483 DUP_STRING(ctx->ctx, must_p->ref, must->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100484 COMPILE_ARRAY_GOTO(ctx, must_p->exts, must->exts, options, u, lys_compile_ext, ret, done);
485
486done:
487 return ret;
488}
489
490static LY_ERR
491lys_compile_import(struct lysc_ctx *ctx, struct lysp_import *imp_p, int options, struct lysc_import *imp)
492{
493 unsigned int u;
494 struct lys_module *mod = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100495 LY_ERR ret = LY_SUCCESS;
496
497 DUP_STRING(ctx->ctx, imp_p->prefix, imp->prefix);
498 COMPILE_ARRAY_GOTO(ctx, imp_p->exts, imp->exts, options, u, lys_compile_ext, ret, done);
499 imp->module = imp_p->module;
500
Radek Krejci7f2a5362018-11-28 13:05:37 +0100501 /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs.
502 * The compiled version is needed only for augments, deviates and leafrefs, so they are checked (and added,
Radek Krejci0e5d8382018-11-28 16:37:53 +0100503 * if needed) when these nodes are finally being instantiated and validated at the end of schema compilation. */
Radek Krejci19a96102018-11-15 13:38:09 +0100504 if (!imp->module->parsed) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100505 /* try to use filepath if present */
506 if (imp->module->filepath) {
507 mod = (struct lys_module*)lys_parse_path(ctx->ctx, imp->module->filepath,
508 !strcmp(&imp->module->filepath[strlen(imp->module->filepath - 4)], ".yin") ? LYS_IN_YIN : LYS_IN_YANG);
Radek Krejci19a96102018-11-15 13:38:09 +0100509 if (mod != imp->module) {
510 LOGERR(ctx->ctx, LY_EINT, "Filepath \"%s\" of the module \"%s\" does not match.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100511 imp->module->filepath, imp->module->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100512 mod = NULL;
513 }
514 }
515 if (!mod) {
Radek Krejci0af46292019-01-11 16:02:31 +0100516 if (lysp_load_module(ctx->ctx, imp->module->name, imp->module->revision, 0, 1, &mod)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100517 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 +0100518 imp->module->name, ctx->mod->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100519 return LY_ENOTFOUND;
520 }
521 }
Radek Krejci19a96102018-11-15 13:38:09 +0100522 }
523
524done:
525 return ret;
526}
527
528static LY_ERR
Radek Krejcid05cbd92018-12-05 14:26:40 +0100529lys_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 +0100530{
531 unsigned int u;
532 LY_ERR ret = LY_SUCCESS;
533
Radek Krejcid05cbd92018-12-05 14:26:40 +0100534 COMPILE_CHECK_UNIQUENESS(ctx, idents, name, ident, "identity", ident_p->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100535 DUP_STRING(ctx->ctx, ident_p->name, ident->name);
Radek Krejcic8b31002019-01-08 10:24:45 +0100536 DUP_STRING(ctx->ctx, ident_p->ref, ident->dsc);
537 DUP_STRING(ctx->ctx, ident_p->ref, ident->dsc);
Radek Krejci19a96102018-11-15 13:38:09 +0100538 COMPILE_ARRAY_GOTO(ctx, ident_p->iffeatures, ident->iffeatures, options, u, lys_compile_iffeature, ret, done);
539 /* backlings (derived) can be added no sooner than when all the identities in the current module are present */
540 COMPILE_ARRAY_GOTO(ctx, ident_p->exts, ident->exts, options, u, lys_compile_ext, ret, done);
541 ident->flags = ident_p->flags;
542
543done:
544 return ret;
545}
546
Radek Krejcia3045382018-11-22 14:30:31 +0100547/**
548 * @brief Find and process the referenced base identities from another identity or identityref
549 *
550 * For bases in identity se backlinks to them from the base identities. For identityref, store
551 * the array of pointers to the base identities. So one of the ident or bases parameter must be set
552 * to distinguish these two use cases.
553 *
554 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
555 * @param[in] bases_p Array of names (including prefix if necessary) of base identities.
556 * @param[in] ident Referencing identity to work with.
557 * @param[in] bases Array of bases of identityref to fill in.
558 * @return LY_ERR value.
559 */
Radek Krejci19a96102018-11-15 13:38:09 +0100560static LY_ERR
Radek Krejci555cb5b2018-11-16 14:54:33 +0100561lys_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 +0100562{
Radek Krejci555cb5b2018-11-16 14:54:33 +0100563 unsigned int u, v;
Radek Krejci19a96102018-11-15 13:38:09 +0100564 const char *s, *name;
Radek Krejcie86bf772018-12-14 11:39:53 +0100565 struct lys_module *mod;
Radek Krejci555cb5b2018-11-16 14:54:33 +0100566 struct lysc_ident **idref;
567
568 assert(ident || bases);
569
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100570 if (LY_ARRAY_SIZE(bases_p) > 1 && ctx->mod_def->version < 2) {
Radek Krejci555cb5b2018-11-16 14:54:33 +0100571 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
572 "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type");
573 return LY_EVALID;
574 }
575
576 for (u = 0; u < LY_ARRAY_SIZE(bases_p); ++u) {
577 s = strchr(bases_p[u], ':');
578 if (s) {
579 /* prefixed identity */
580 name = &s[1];
Radek Krejcie86bf772018-12-14 11:39:53 +0100581 mod = lys_module_find_prefix(ctx->mod_def, bases_p[u], s - bases_p[u]);
Radek Krejci555cb5b2018-11-16 14:54:33 +0100582 } else {
583 name = bases_p[u];
Radek Krejcie86bf772018-12-14 11:39:53 +0100584 mod = ctx->mod_def;
Radek Krejci555cb5b2018-11-16 14:54:33 +0100585 }
586 if (!mod) {
587 if (ident) {
588 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
589 "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name);
590 } else {
591 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
592 "Invalid prefix used for base (%s) of identityref.", bases_p[u]);
593 }
594 return LY_EVALID;
595 }
596 idref = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +0100597 if (mod->compiled && mod->compiled->identities) {
598 for (v = 0; v < LY_ARRAY_SIZE(mod->compiled->identities); ++v) {
599 if (!strcmp(name, mod->compiled->identities[v].name)) {
Radek Krejci555cb5b2018-11-16 14:54:33 +0100600 if (ident) {
601 /* we have match! store the backlink */
Radek Krejcie86bf772018-12-14 11:39:53 +0100602 LY_ARRAY_NEW_RET(ctx->ctx, mod->compiled->identities[v].derived, idref, LY_EMEM);
Radek Krejci555cb5b2018-11-16 14:54:33 +0100603 *idref = ident;
604 } else {
605 /* we have match! store the found identity */
606 LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +0100607 *idref = &mod->compiled->identities[v];
Radek Krejci555cb5b2018-11-16 14:54:33 +0100608 }
609 break;
610 }
611 }
612 }
613 if (!idref || !(*idref)) {
614 if (ident) {
615 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
616 "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name);
617 } else {
618 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
619 "Unable to find base (%s) of identityref.", bases_p[u]);
620 }
621 return LY_EVALID;
622 }
623 }
624 return LY_SUCCESS;
625}
626
Radek Krejcia3045382018-11-22 14:30:31 +0100627/**
628 * @brief For the given array of identities, set the backlinks from all their base identities.
629 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
630 * @param[in] idents_p Array of identities definitions from the parsed schema structure.
631 * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set.
632 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
633 */
Radek Krejci555cb5b2018-11-16 14:54:33 +0100634static LY_ERR
635lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents)
636{
637 unsigned int i;
Radek Krejci19a96102018-11-15 13:38:09 +0100638
639 for (i = 0; i < LY_ARRAY_SIZE(idents_p); ++i) {
640 if (!idents_p[i].bases) {
641 continue;
642 }
Radek Krejci555cb5b2018-11-16 14:54:33 +0100643 LY_CHECK_RET(lys_compile_identity_bases(ctx, idents_p[i].bases, &idents[i], NULL));
Radek Krejci19a96102018-11-15 13:38:09 +0100644 }
645 return LY_SUCCESS;
646}
647
Radek Krejci0af46292019-01-11 16:02:31 +0100648LY_ERR
649lys_feature_precompile(struct ly_ctx *ctx, struct lysp_feature *features_p, struct lysc_feature **features)
650{
651 unsigned int offset = 0, u;
652 struct lysc_ctx context = {0};
653
654 assert(ctx);
655 context.ctx = ctx;
656
657 if (!features_p) {
658 return LY_SUCCESS;
659 }
660 if (*features) {
661 offset = LY_ARRAY_SIZE(*features);
662 }
663
664 LY_ARRAY_CREATE_RET(ctx, *features, LY_ARRAY_SIZE(features_p), LY_EMEM);
665 LY_ARRAY_FOR(features_p, u) {
666 LY_ARRAY_INCREMENT(*features);
667 COMPILE_CHECK_UNIQUENESS(&context, *features, name, &(*features)[offset + u], "feature", features_p[u].name);
668 DUP_STRING(ctx, features_p[u].name, (*features)[offset + u].name);
669 DUP_STRING(ctx, features_p[u].dsc, (*features)[offset + u].dsc);
670 DUP_STRING(ctx, features_p[u].ref, (*features)[offset + u].ref);
671 (*features)[offset + u].flags = features_p[u].flags;
672 }
673
674 return LY_SUCCESS;
675}
676
Radek Krejcia3045382018-11-22 14:30:31 +0100677/**
Radek Krejci0af46292019-01-11 16:02:31 +0100678 * @brief Create pre-compiled features array.
679 *
680 * See lys_feature_precompile() for more details.
681 *
Radek Krejcia3045382018-11-22 14:30:31 +0100682 * @param[in] ctx Compile context.
683 * @param[in] feature_p Parsed feature definition to compile.
684 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
Radek Krejci0af46292019-01-11 16:02:31 +0100685 * @param[in,out] features List of already (pre)compiled features to find the corresponding precompiled feature structure.
Radek Krejcia3045382018-11-22 14:30:31 +0100686 * @return LY_ERR value.
687 */
Radek Krejci19a96102018-11-15 13:38:09 +0100688static LY_ERR
Radek Krejci0af46292019-01-11 16:02:31 +0100689lys_feature_precompile_finish(struct lysc_ctx *ctx, struct lysp_feature *feature_p, int options, struct lysc_feature *features)
Radek Krejci19a96102018-11-15 13:38:09 +0100690{
Radek Krejci0af46292019-01-11 16:02:31 +0100691 unsigned int u, v, x;
692 struct lysc_feature *feature, **df;
Radek Krejci19a96102018-11-15 13:38:09 +0100693 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +0100694
Radek Krejci0af46292019-01-11 16:02:31 +0100695 /* find the preprecompiled feature */
696 LY_ARRAY_FOR(features, x) {
697 if (strcmp(features[x].name, feature_p->name)) {
698 continue;
699 }
700 feature = &features[x];
Radek Krejci19a96102018-11-15 13:38:09 +0100701
Radek Krejci0af46292019-01-11 16:02:31 +0100702 /* finish compilation started in lys_feature_precompile() */
703 COMPILE_ARRAY_GOTO(ctx, feature_p->exts, feature->exts, options, u, lys_compile_ext, ret, done);
704 COMPILE_ARRAY_GOTO(ctx, feature_p->iffeatures, feature->iffeatures, options, u, lys_compile_iffeature, ret, done);
705 if (feature->iffeatures) {
706 for (u = 0; u < LY_ARRAY_SIZE(feature->iffeatures); ++u) {
707 if (feature->iffeatures[u].features) {
708 for (v = 0; v < LY_ARRAY_SIZE(feature->iffeatures[u].features); ++v) {
709 /* add itself into the dependants list */
710 LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM);
711 *df = feature;
712 }
713 /* TODO check for circular dependency */
Radek Krejci19a96102018-11-15 13:38:09 +0100714 }
Radek Krejci19a96102018-11-15 13:38:09 +0100715 }
716 }
Radek Krejci0af46292019-01-11 16:02:31 +0100717 done:
718 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +0100719 }
Radek Krejci0af46292019-01-11 16:02:31 +0100720
721 LOGINT(ctx->ctx);
722 return LY_EINT;
Radek Krejci19a96102018-11-15 13:38:09 +0100723}
724
Radek Krejci95710c92019-02-11 15:49:55 +0100725static void
726lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod)
727{
728 unsigned int u, v;
729
730 /* keep the off_features list until the complete lys_module is freed */
731 mod->off_features = mod->compiled->features;
732 mod->compiled->features = NULL;
733
734 /* in the off_features list, remove all the parts (from finished compiling process)
735 * which may points into the data being freed here */
736 LY_ARRAY_FOR(mod->off_features, u) {
737 LY_ARRAY_FOR(mod->off_features[u].iffeatures, v) {
738 lysc_iffeature_free(ctx->ctx, &mod->off_features[u].iffeatures[v]);
739 }
740 LY_ARRAY_FREE(mod->off_features[u].iffeatures);
741 mod->off_features[u].iffeatures = NULL;
742
743 LY_ARRAY_FOR(mod->off_features[u].exts, v) {
744 lysc_ext_instance_free(ctx->ctx, &(mod->off_features[u].exts)[v]);
745 }
746 LY_ARRAY_FREE(mod->off_features[u].exts);
747 mod->off_features[u].exts = NULL;
748 }
749}
750
Radek Krejcia3045382018-11-22 14:30:31 +0100751/**
752 * @brief Validate and normalize numeric value from a range definition.
753 * @param[in] ctx Compile context.
754 * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to
755 * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into
756 * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from
757 * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100.
758 * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64.
759 * @param[in] value String value of the range boundary.
760 * @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.
761 * @param[out] valcopy NULL-terminated string with the numeric value to parse and store.
762 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value).
763 */
Radek Krejci19a96102018-11-15 13:38:09 +0100764static LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +0100765range_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 +0100766{
Radek Krejci6cba4292018-11-15 17:33:29 +0100767 size_t fraction = 0, size;
768
Radek Krejci19a96102018-11-15 13:38:09 +0100769 *len = 0;
770
771 assert(value);
772 /* parse value */
773 if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) {
774 return LY_EVALID;
775 }
776
777 if ((value[*len] == '-') || (value[*len] == '+')) {
778 ++(*len);
779 }
780
781 while (isdigit(value[*len])) {
782 ++(*len);
783 }
784
785 if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) {
Radek Krejci6cba4292018-11-15 17:33:29 +0100786 if (basetype == LY_TYPE_DEC64) {
787 goto decimal;
788 } else {
789 *valcopy = strndup(value, *len);
790 return LY_SUCCESS;
791 }
Radek Krejci19a96102018-11-15 13:38:09 +0100792 }
793 fraction = *len;
794
795 ++(*len);
796 while (isdigit(value[*len])) {
797 ++(*len);
798 }
799
Radek Krejci6cba4292018-11-15 17:33:29 +0100800 if (basetype == LY_TYPE_DEC64) {
801decimal:
802 assert(frdigits);
803 if (*len - 1 - fraction > frdigits) {
804 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
805 "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.",
806 *len, value, frdigits);
807 return LY_EINVAL;
808 }
809 if (fraction) {
810 size = (*len) + (frdigits - ((*len) - 1 - fraction));
811 } else {
812 size = (*len) + frdigits + 1;
813 }
814 *valcopy = malloc(size * sizeof **valcopy);
Radek Krejci19a96102018-11-15 13:38:09 +0100815 LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM);
816
Radek Krejci6cba4292018-11-15 17:33:29 +0100817 (*valcopy)[size - 1] = '\0';
818 if (fraction) {
819 memcpy(&(*valcopy)[0], &value[0], fraction);
820 memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction));
821 memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction));
822 } else {
823 memcpy(&(*valcopy)[0], &value[0], *len);
824 memset(&(*valcopy)[*len], '0', frdigits);
825 }
Radek Krejci19a96102018-11-15 13:38:09 +0100826 }
827 return LY_SUCCESS;
828}
829
Radek Krejcia3045382018-11-22 14:30:31 +0100830/**
831 * @brief Check that values in range are in ascendant order.
832 * @param[in] unsigned_value Flag to note that we are working with unsigned values.
Radek Krejci5969f272018-11-23 10:03:58 +0100833 * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous,
834 * max can be also equal.
Radek Krejcia3045382018-11-22 14:30:31 +0100835 * @param[in] value Current value to check.
836 * @param[in] prev_value The last seen value.
837 * @return LY_SUCCESS or LY_EEXIST for invalid order.
838 */
Radek Krejci19a96102018-11-15 13:38:09 +0100839static LY_ERR
Radek Krejci5969f272018-11-23 10:03:58 +0100840range_part_check_ascendancy(int unsigned_value, int max, int64_t value, int64_t prev_value)
Radek Krejci19a96102018-11-15 13:38:09 +0100841{
842 if (unsigned_value) {
Radek Krejci5969f272018-11-23 10:03:58 +0100843 if ((max && (uint64_t)prev_value > (uint64_t)value) || (!max && (uint64_t)prev_value >= (uint64_t)value)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100844 return LY_EEXIST;
845 }
846 } else {
Radek Krejci5969f272018-11-23 10:03:58 +0100847 if ((max && prev_value > value) || (!max && prev_value >= value)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100848 return LY_EEXIST;
849 }
850 }
851 return LY_SUCCESS;
852}
853
Radek Krejcia3045382018-11-22 14:30:31 +0100854/**
855 * @brief Set min/max value of the range part.
856 * @param[in] ctx Compile context.
857 * @param[in] part Range part structure to fill.
858 * @param[in] max Flag to distinguish if storing min or max value.
859 * @param[in] prev The last seen value to check that all values in range are specified in ascendant order.
860 * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules.
861 * @param[in] first Flag for the first value of the range to avoid ascendancy order.
862 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
863 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
Radek Krejci5969f272018-11-23 10:03:58 +0100864 * @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 +0100865 * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set.
866 * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number),
867 * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the
868 * frdigits value), LY_EMEM.
869 */
Radek Krejci19a96102018-11-15 13:38:09 +0100870static LY_ERR
871range_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 +0100872 uint8_t frdigits, struct lysc_range *base_range, const char **value)
Radek Krejci19a96102018-11-15 13:38:09 +0100873{
874 LY_ERR ret = LY_SUCCESS;
875 char *valcopy = NULL;
876 size_t len;
877
878 if (value) {
Radek Krejci6cba4292018-11-15 17:33:29 +0100879 ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy);
Radek Krejci5969f272018-11-23 10:03:58 +0100880 LY_CHECK_GOTO(ret, finalize);
881 }
882 if (!valcopy && base_range) {
883 if (max) {
884 part->max_64 = base_range->parts[LY_ARRAY_SIZE(base_range->parts) - 1].max_64;
885 } else {
886 part->min_64 = base_range->parts[0].min_64;
887 }
888 if (!first) {
889 ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev);
890 }
891 goto finalize;
Radek Krejci19a96102018-11-15 13:38:09 +0100892 }
893
894 switch (basetype) {
Radek Krejci19a96102018-11-15 13:38:09 +0100895 case LY_TYPE_INT8: /* range */
896 if (valcopy) {
897 ret = ly_parse_int(valcopy, INT64_C(-128), INT64_C(127), 10, max ? &part->max_64 : &part->min_64);
898 } else if (max) {
899 part->max_64 = INT64_C(127);
900 } else {
901 part->min_64 = INT64_C(-128);
902 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100903 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +0100904 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +0100905 }
906 break;
907 case LY_TYPE_INT16: /* range */
908 if (valcopy) {
909 ret = ly_parse_int(valcopy, INT64_C(-32768), INT64_C(32767), 10, max ? &part->max_64 : &part->min_64);
910 } else if (max) {
911 part->max_64 = INT64_C(32767);
912 } else {
913 part->min_64 = INT64_C(-32768);
914 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100915 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +0100916 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +0100917 }
918 break;
919 case LY_TYPE_INT32: /* range */
920 if (valcopy) {
921 ret = ly_parse_int(valcopy, INT64_C(-2147483648), INT64_C(2147483647), 10, max ? &part->max_64 : &part->min_64);
922 } else if (max) {
923 part->max_64 = INT64_C(2147483647);
924 } else {
925 part->min_64 = INT64_C(-2147483648);
926 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100927 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +0100928 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +0100929 }
930 break;
931 case LY_TYPE_INT64: /* range */
Radek Krejci25cfef72018-11-23 14:15:52 +0100932 case LY_TYPE_DEC64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +0100933 if (valcopy) {
934 ret = ly_parse_int(valcopy, INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807), 10,
935 max ? &part->max_64 : &part->min_64);
936 } else if (max) {
937 part->max_64 = INT64_C(9223372036854775807);
938 } else {
939 part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1);
940 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100941 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +0100942 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +0100943 }
944 break;
945 case LY_TYPE_UINT8: /* range */
946 if (valcopy) {
947 ret = ly_parse_uint(valcopy, UINT64_C(255), 10, max ? &part->max_u64 : &part->min_u64);
948 } else if (max) {
949 part->max_u64 = UINT64_C(255);
950 } else {
951 part->min_u64 = UINT64_C(0);
952 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100953 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +0100954 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +0100955 }
956 break;
957 case LY_TYPE_UINT16: /* range */
958 if (valcopy) {
959 ret = ly_parse_uint(valcopy, UINT64_C(65535), 10, max ? &part->max_u64 : &part->min_u64);
960 } else if (max) {
961 part->max_u64 = UINT64_C(65535);
962 } else {
963 part->min_u64 = UINT64_C(0);
964 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100965 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +0100966 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +0100967 }
968 break;
969 case LY_TYPE_UINT32: /* range */
970 if (valcopy) {
971 ret = ly_parse_uint(valcopy, UINT64_C(4294967295), 10, max ? &part->max_u64 : &part->min_u64);
972 } else if (max) {
973 part->max_u64 = UINT64_C(4294967295);
974 } else {
975 part->min_u64 = UINT64_C(0);
976 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100977 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +0100978 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +0100979 }
980 break;
981 case LY_TYPE_UINT64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +0100982 case LY_TYPE_STRING: /* length */
Radek Krejci25cfef72018-11-23 14:15:52 +0100983 case LY_TYPE_BINARY: /* length */
Radek Krejci19a96102018-11-15 13:38:09 +0100984 if (valcopy) {
985 ret = ly_parse_uint(valcopy, UINT64_C(18446744073709551615), 10, max ? &part->max_u64 : &part->min_u64);
986 } else if (max) {
987 part->max_u64 = UINT64_C(18446744073709551615);
988 } else {
989 part->min_u64 = UINT64_C(0);
990 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100991 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +0100992 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +0100993 }
994 break;
995 default:
996 LOGINT(ctx->ctx);
997 ret = LY_EINT;
998 }
999
Radek Krejci5969f272018-11-23 10:03:58 +01001000finalize:
Radek Krejci19a96102018-11-15 13:38:09 +01001001 if (ret == LY_EDENIED) {
1002 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1003 "Invalid %s restriction - value \"%s\" does not fit the type limitations.",
1004 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1005 } else if (ret == LY_EVALID) {
1006 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1007 "Invalid %s restriction - invalid value \"%s\".",
1008 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1009 } else if (ret == LY_EEXIST) {
1010 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1011 "Invalid %s restriction - values are not in ascending order (%s).",
Radek Krejci6cba4292018-11-15 17:33:29 +01001012 length_restr ? "length" : "range",
Radek Krejci5969f272018-11-23 10:03:58 +01001013 (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min");
Radek Krejci19a96102018-11-15 13:38:09 +01001014 } else if (!ret && value) {
1015 *value = *value + len;
1016 }
1017 free(valcopy);
1018 return ret;
1019}
1020
Radek Krejcia3045382018-11-22 14:30:31 +01001021/**
1022 * @brief Compile the parsed range restriction.
1023 * @param[in] ctx Compile context.
1024 * @param[in] range_p Parsed range structure to compile.
1025 * @param[in] basetype Base YANG built-in type of the node with the range restriction.
1026 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1027 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
1028 * @param[in] base_range Range restriction of the type from which the current type is derived. The current
1029 * range restriction must be more restrictive than the base_range.
1030 * @param[in,out] range Pointer to the created current range structure.
1031 * @return LY_ERR value.
1032 */
Radek Krejci19a96102018-11-15 13:38:09 +01001033static LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001034lys_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 +01001035 struct lysc_range *base_range, struct lysc_range **range)
1036{
1037 LY_ERR ret = LY_EVALID;
1038 const char *expr;
1039 struct lysc_range_part *parts = NULL, *part;
1040 int range_expected = 0, uns;
1041 unsigned int parts_done = 0, u, v;
1042
1043 assert(range);
1044 assert(range_p);
1045
1046 expr = range_p->arg;
1047 while(1) {
1048 if (isspace(*expr)) {
1049 ++expr;
1050 } else if (*expr == '\0') {
1051 if (range_expected) {
1052 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1053 "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).",
1054 length_restr ? "length" : "range", range_p->arg);
1055 goto cleanup;
1056 } else if (!parts || parts_done == LY_ARRAY_SIZE(parts)) {
1057 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1058 "Invalid %s restriction - unexpected end of the expression (%s).",
1059 length_restr ? "length" : "range", range_p->arg);
1060 goto cleanup;
1061 }
1062 parts_done++;
1063 break;
1064 } else if (!strncmp(expr, "min", 3)) {
1065 if (parts) {
1066 /* min cannot be used elsewhere than in the first part */
1067 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1068 "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range",
1069 expr - range_p->arg, range_p->arg);
1070 goto cleanup;
1071 }
1072 expr += 3;
1073
1074 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Radek Krejci5969f272018-11-23 10:03:58 +01001075 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 +01001076 part->max_64 = part->min_64;
1077 } else if (*expr == '|') {
1078 if (!parts || range_expected) {
1079 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1080 "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr);
1081 goto cleanup;
1082 }
1083 expr++;
1084 parts_done++;
1085 /* process next part of the expression */
1086 } else if (!strncmp(expr, "..", 2)) {
1087 expr += 2;
1088 while (isspace(*expr)) {
1089 expr++;
1090 }
1091 if (!parts || LY_ARRAY_SIZE(parts) == parts_done) {
1092 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1093 "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range");
1094 goto cleanup;
1095 }
1096 /* continue expecting the upper boundary */
1097 range_expected = 1;
1098 } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) {
1099 /* number */
1100 if (range_expected) {
1101 part = &parts[LY_ARRAY_SIZE(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001102 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 +01001103 range_expected = 0;
1104 } else {
1105 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
1106 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 +01001107 basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001108 part->max_64 = part->min_64;
1109 }
1110
1111 /* continue with possible another expression part */
1112 } else if (!strncmp(expr, "max", 3)) {
1113 expr += 3;
1114 while (isspace(*expr)) {
1115 expr++;
1116 }
1117 if (*expr != '\0') {
1118 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).",
1119 length_restr ? "length" : "range", expr);
1120 goto cleanup;
1121 }
1122 if (range_expected) {
1123 part = &parts[LY_ARRAY_SIZE(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001124 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 +01001125 range_expected = 0;
1126 } else {
1127 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
1128 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 +01001129 basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001130 part->min_64 = part->max_64;
1131 }
1132 } else {
1133 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).",
1134 length_restr ? "length" : "range", expr);
1135 goto cleanup;
1136 }
1137 }
1138
1139 /* check with the previous range/length restriction */
1140 if (base_range) {
1141 switch (basetype) {
1142 case LY_TYPE_BINARY:
1143 case LY_TYPE_UINT8:
1144 case LY_TYPE_UINT16:
1145 case LY_TYPE_UINT32:
1146 case LY_TYPE_UINT64:
1147 case LY_TYPE_STRING:
1148 uns = 1;
1149 break;
1150 case LY_TYPE_DEC64:
1151 case LY_TYPE_INT8:
1152 case LY_TYPE_INT16:
1153 case LY_TYPE_INT32:
1154 case LY_TYPE_INT64:
1155 uns = 0;
1156 break;
1157 default:
1158 LOGINT(ctx->ctx);
1159 ret = LY_EINT;
1160 goto cleanup;
1161 }
1162 for (u = v = 0; u < parts_done && v < LY_ARRAY_SIZE(base_range->parts); ++u) {
1163 if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) {
1164 goto baseerror;
1165 }
1166 /* current lower bound is not lower than the base */
1167 if (base_range->parts[v].min_64 == base_range->parts[v].max_64) {
1168 /* base has single value */
1169 if (base_range->parts[v].min_64 == parts[u].min_64) {
1170 /* both lower bounds are the same */
1171 if (parts[u].min_64 != parts[u].max_64) {
1172 /* current continues with a range */
1173 goto baseerror;
1174 } else {
1175 /* equal single values, move both forward */
1176 ++v;
1177 continue;
1178 }
1179 } else {
1180 /* base is single value lower than current range, so the
1181 * value from base range is removed in the current,
1182 * move only base and repeat checking */
1183 ++v;
1184 --u;
1185 continue;
1186 }
1187 } else {
1188 /* base is the range */
1189 if (parts[u].min_64 == parts[u].max_64) {
1190 /* current is a single value */
1191 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1192 /* current is behind the base range, so base range is omitted,
1193 * move the base and keep the current for further check */
1194 ++v;
1195 --u;
1196 } /* else it is within the base range, so move the current, but keep the base */
1197 continue;
1198 } else {
1199 /* both are ranges - check the higher bound, the lower was already checked */
1200 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1201 /* higher bound is higher than the current higher bound */
1202 if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) {
1203 /* but the current lower bound is also higher, so the base range is omitted,
1204 * continue with the same current, but move the base */
1205 --u;
1206 ++v;
1207 continue;
1208 }
1209 /* current range starts within the base range but end behind it */
1210 goto baseerror;
1211 } else {
1212 /* current range is smaller than the base,
1213 * move current, but stay with the base */
1214 continue;
1215 }
1216 }
1217 }
1218 }
1219 if (u != parts_done) {
1220baseerror:
1221 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1222 "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.",
1223 length_restr ? "length" : "range", range_p->arg);
1224 goto cleanup;
1225 }
1226 }
1227
1228 if (!(*range)) {
1229 *range = calloc(1, sizeof **range);
1230 LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM);
1231 }
1232
Radek Krejcic8b31002019-01-08 10:24:45 +01001233 /* we rewrite the following values as the types chain is being processed */
Radek Krejci19a96102018-11-15 13:38:09 +01001234 if (range_p->eapptag) {
1235 lydict_remove(ctx->ctx, (*range)->eapptag);
1236 (*range)->eapptag = lydict_insert(ctx->ctx, range_p->eapptag, 0);
1237 }
1238 if (range_p->emsg) {
1239 lydict_remove(ctx->ctx, (*range)->emsg);
1240 (*range)->emsg = lydict_insert(ctx->ctx, range_p->emsg, 0);
1241 }
Radek Krejcic8b31002019-01-08 10:24:45 +01001242 if (range_p->dsc) {
1243 lydict_remove(ctx->ctx, (*range)->dsc);
1244 (*range)->dsc = lydict_insert(ctx->ctx, range_p->dsc, 0);
1245 }
1246 if (range_p->ref) {
1247 lydict_remove(ctx->ctx, (*range)->ref);
1248 (*range)->ref = lydict_insert(ctx->ctx, range_p->ref, 0);
1249 }
Radek Krejci19a96102018-11-15 13:38:09 +01001250 /* extensions are taken only from the last range by the caller */
1251
1252 (*range)->parts = parts;
1253 parts = NULL;
1254 ret = LY_SUCCESS;
1255cleanup:
1256 /* TODO clean up */
1257 LY_ARRAY_FREE(parts);
1258
1259 return ret;
1260}
1261
1262/**
1263 * @brief Checks pattern syntax.
1264 *
Radek Krejcia3045382018-11-22 14:30:31 +01001265 * @param[in] ctx Compile context.
Radek Krejci19a96102018-11-15 13:38:09 +01001266 * @param[in] pattern Pattern to check.
Radek Krejcia3045382018-11-22 14:30:31 +01001267 * @param[in,out] pcre_precomp Precompiled PCRE pattern. If NULL, the compiled information used to validate pattern are freed.
1268 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID.
Radek Krejci19a96102018-11-15 13:38:09 +01001269 */
1270static LY_ERR
1271lys_compile_type_pattern_check(struct lysc_ctx *ctx, const char *pattern, pcre **pcre_precomp)
1272{
1273 int idx, idx2, start, end, err_offset, count;
1274 char *perl_regex, *ptr;
1275 const char *err_msg, *orig_ptr;
1276 pcre *precomp;
1277#define URANGE_LEN 19
1278 char *ublock2urange[][2] = {
1279 {"BasicLatin", "[\\x{0000}-\\x{007F}]"},
1280 {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"},
1281 {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"},
1282 {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"},
1283 {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"},
1284 {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"},
1285 {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"},
1286 {"Greek", "[\\x{0370}-\\x{03FF}]"},
1287 {"Cyrillic", "[\\x{0400}-\\x{04FF}]"},
1288 {"Armenian", "[\\x{0530}-\\x{058F}]"},
1289 {"Hebrew", "[\\x{0590}-\\x{05FF}]"},
1290 {"Arabic", "[\\x{0600}-\\x{06FF}]"},
1291 {"Syriac", "[\\x{0700}-\\x{074F}]"},
1292 {"Thaana", "[\\x{0780}-\\x{07BF}]"},
1293 {"Devanagari", "[\\x{0900}-\\x{097F}]"},
1294 {"Bengali", "[\\x{0980}-\\x{09FF}]"},
1295 {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"},
1296 {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"},
1297 {"Oriya", "[\\x{0B00}-\\x{0B7F}]"},
1298 {"Tamil", "[\\x{0B80}-\\x{0BFF}]"},
1299 {"Telugu", "[\\x{0C00}-\\x{0C7F}]"},
1300 {"Kannada", "[\\x{0C80}-\\x{0CFF}]"},
1301 {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"},
1302 {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"},
1303 {"Thai", "[\\x{0E00}-\\x{0E7F}]"},
1304 {"Lao", "[\\x{0E80}-\\x{0EFF}]"},
1305 {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"},
1306 {"Myanmar", "[\\x{1000}-\\x{109F}]"},
1307 {"Georgian", "[\\x{10A0}-\\x{10FF}]"},
1308 {"HangulJamo", "[\\x{1100}-\\x{11FF}]"},
1309 {"Ethiopic", "[\\x{1200}-\\x{137F}]"},
1310 {"Cherokee", "[\\x{13A0}-\\x{13FF}]"},
1311 {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"},
1312 {"Ogham", "[\\x{1680}-\\x{169F}]"},
1313 {"Runic", "[\\x{16A0}-\\x{16FF}]"},
1314 {"Khmer", "[\\x{1780}-\\x{17FF}]"},
1315 {"Mongolian", "[\\x{1800}-\\x{18AF}]"},
1316 {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"},
1317 {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"},
1318 {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"},
1319 {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"},
1320 {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"},
1321 {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"},
1322 {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"},
1323 {"NumberForms", "[\\x{2150}-\\x{218F}]"},
1324 {"Arrows", "[\\x{2190}-\\x{21FF}]"},
1325 {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"},
1326 {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"},
1327 {"ControlPictures", "[\\x{2400}-\\x{243F}]"},
1328 {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"},
1329 {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"},
1330 {"BoxDrawing", "[\\x{2500}-\\x{257F}]"},
1331 {"BlockElements", "[\\x{2580}-\\x{259F}]"},
1332 {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"},
1333 {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"},
1334 {"Dingbats", "[\\x{2700}-\\x{27BF}]"},
1335 {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"},
1336 {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"},
1337 {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"},
1338 {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"},
1339 {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"},
1340 {"Hiragana", "[\\x{3040}-\\x{309F}]"},
1341 {"Katakana", "[\\x{30A0}-\\x{30FF}]"},
1342 {"Bopomofo", "[\\x{3100}-\\x{312F}]"},
1343 {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"},
1344 {"Kanbun", "[\\x{3190}-\\x{319F}]"},
1345 {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"},
1346 {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"},
1347 {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"},
1348 {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"},
1349 {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"},
1350 {"YiSyllables", "[\\x{A000}-\\x{A48F}]"},
1351 {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"},
1352 {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"},
1353 {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"},
1354 {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"},
1355 {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"},
1356 {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"},
1357 {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"},
1358 {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"},
1359 {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"},
1360 {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"},
1361 {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"},
1362 {NULL, NULL}
1363 };
1364
1365 /* adjust the expression to a Perl equivalent
1366 * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */
1367
1368 /* we need to replace all "$" with "\$", count them now */
1369 for (count = 0, ptr = strpbrk(pattern, "^$"); ptr; ++count, ptr = strpbrk(ptr + 1, "^$"));
1370
1371 perl_regex = malloc((strlen(pattern) + 4 + count) * sizeof(char));
1372 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx), LY_EMEM);
1373 perl_regex[0] = '\0';
1374
1375 ptr = perl_regex;
1376
1377 if (strncmp(pattern + strlen(pattern) - 2, ".*", 2)) {
1378 /* we will add line-end anchoring */
1379 ptr[0] = '(';
1380 ++ptr;
1381 }
1382
1383 for (orig_ptr = pattern; orig_ptr[0]; ++orig_ptr) {
1384 if (orig_ptr[0] == '$') {
1385 ptr += sprintf(ptr, "\\$");
1386 } else if (orig_ptr[0] == '^') {
1387 ptr += sprintf(ptr, "\\^");
1388 } else {
1389 ptr[0] = orig_ptr[0];
1390 ++ptr;
1391 }
1392 }
1393
1394 if (strncmp(pattern + strlen(pattern) - 2, ".*", 2)) {
1395 ptr += sprintf(ptr, ")$");
1396 } else {
1397 ptr[0] = '\0';
1398 ++ptr;
1399 }
1400
1401 /* substitute Unicode Character Blocks with exact Character Ranges */
1402 while ((ptr = strstr(perl_regex, "\\p{Is"))) {
1403 start = ptr - perl_regex;
1404
1405 ptr = strchr(ptr, '}');
1406 if (!ptr) {
1407 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP,
1408 pattern, perl_regex + start + 2, "unterminated character property");
1409 free(perl_regex);
1410 return LY_EVALID;
1411 }
1412 end = (ptr - perl_regex) + 1;
1413
1414 /* need more space */
1415 if (end - start < URANGE_LEN) {
1416 perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1);
1417 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx); free(perl_regex), LY_EMEM);
1418 }
1419
1420 /* find our range */
1421 for (idx = 0; ublock2urange[idx][0]; ++idx) {
1422 if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) {
1423 break;
1424 }
1425 }
1426 if (!ublock2urange[idx][0]) {
1427 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP,
1428 pattern, perl_regex + start + 5, "unknown block name");
1429 free(perl_regex);
1430 return LY_EVALID;
1431 }
1432
1433 /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */
1434 for (idx2 = 0, count = 0; idx2 < start; ++idx2) {
1435 if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1436 ++count;
1437 }
1438 if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1439 --count;
1440 }
1441 }
1442 if (count) {
1443 /* skip brackets */
1444 memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1);
1445 memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2);
1446 } else {
1447 memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1);
1448 memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN);
1449 }
1450 }
1451
1452 /* must return 0, already checked during parsing */
1453 precomp = pcre_compile(perl_regex, PCRE_UTF8 | PCRE_ANCHORED | PCRE_DOLLAR_ENDONLY | PCRE_NO_AUTO_CAPTURE,
1454 &err_msg, &err_offset, NULL);
1455 if (!precomp) {
1456 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
1457 free(perl_regex);
1458 return LY_EVALID;
1459 }
1460 free(perl_regex);
1461
1462 if (pcre_precomp) {
1463 *pcre_precomp = precomp;
1464 } else {
1465 free(precomp);
1466 }
1467
1468 return LY_SUCCESS;
1469
1470#undef URANGE_LEN
1471}
1472
Radek Krejcia3045382018-11-22 14:30:31 +01001473/**
1474 * @brief Compile parsed pattern restriction in conjunction with the patterns from base type.
1475 * @param[in] ctx Compile context.
1476 * @param[in] patterns_p Array of parsed patterns from the current type to compile.
1477 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
1478 * @param[in] base_patterns Compiled patterns from the type from which the current type is derived.
1479 * Patterns from the base type are inherited to have all the patterns that have to match at one place.
1480 * @param[out] patterns Pointer to the storage for the patterns of the current type.
1481 * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID.
1482 */
Radek Krejci19a96102018-11-15 13:38:09 +01001483static LY_ERR
1484lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p, int options,
1485 struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns)
1486{
1487 struct lysc_pattern **pattern;
1488 unsigned int u, v;
1489 const char *err_msg;
1490 LY_ERR ret = LY_SUCCESS;
1491
1492 /* first, copy the patterns from the base type */
1493 if (base_patterns) {
1494 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
1495 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
1496 }
1497
1498 LY_ARRAY_FOR(patterns_p, u) {
1499 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
1500 *pattern = calloc(1, sizeof **pattern);
1501 ++(*pattern)->refcount;
1502
1503 ret = lys_compile_type_pattern_check(ctx, &patterns_p[u].arg[1], &(*pattern)->expr);
1504 LY_CHECK_RET(ret);
1505 (*pattern)->expr_extra = pcre_study((*pattern)->expr, 0, &err_msg);
1506 if (err_msg) {
1507 LOGWRN(ctx->ctx, "Studying pattern \"%s\" failed (%s).", pattern, err_msg);
1508 }
1509
1510 if (patterns_p[u].arg[0] == 0x15) {
1511 (*pattern)->inverted = 1;
1512 }
1513 DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag);
1514 DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +01001515 DUP_STRING(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc);
1516 DUP_STRING(ctx->ctx, patterns_p[u].ref, (*pattern)->ref);
Radek Krejci19a96102018-11-15 13:38:09 +01001517 COMPILE_ARRAY_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts,
1518 options, v, lys_compile_ext, ret, done);
1519 }
1520done:
1521 return ret;
1522}
1523
Radek Krejcia3045382018-11-22 14:30:31 +01001524/**
1525 * @brief map of the possible restrictions combination for the specific built-in type.
1526 */
Radek Krejci19a96102018-11-15 13:38:09 +01001527static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
1528 0 /* LY_TYPE_UNKNOWN */,
1529 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
Radek Krejci5969f272018-11-23 10:03:58 +01001530 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
1531 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
1532 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
1533 LYS_SET_RANGE /* LY_TYPE_UINT64 */,
1534 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
Radek Krejci19a96102018-11-15 13:38:09 +01001535 LYS_SET_BIT /* LY_TYPE_BITS */,
1536 0 /* LY_TYPE_BOOL */,
1537 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
1538 0 /* LY_TYPE_EMPTY */,
1539 LYS_SET_ENUM /* LY_TYPE_ENUM */,
1540 LYS_SET_BASE /* LY_TYPE_IDENT */,
1541 LYS_SET_REQINST /* LY_TYPE_INST */,
1542 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
Radek Krejci19a96102018-11-15 13:38:09 +01001543 LYS_SET_TYPE /* LY_TYPE_UNION */,
1544 LYS_SET_RANGE /* LY_TYPE_INT8 */,
Radek Krejci19a96102018-11-15 13:38:09 +01001545 LYS_SET_RANGE /* LY_TYPE_INT16 */,
Radek Krejci19a96102018-11-15 13:38:09 +01001546 LYS_SET_RANGE /* LY_TYPE_INT32 */,
Radek Krejci5969f272018-11-23 10:03:58 +01001547 LYS_SET_RANGE /* LY_TYPE_INT64 */
1548};
1549
1550/**
1551 * @brief stringification of the YANG built-in data types
1552 */
1553const char* ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer",
1554 "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration",
1555 "identityref", "instance-identifier", "leafref", "union", "8bit integer", "16bit integer", "32bit integer", "64bit integer"
Radek Krejci19a96102018-11-15 13:38:09 +01001556};
1557
Radek Krejcia3045382018-11-22 14:30:31 +01001558/**
1559 * @brief Compile parsed type's enum structures (for enumeration and bits types).
1560 * @param[in] ctx Compile context.
1561 * @param[in] enums_p Array of the parsed enum structures to compile.
1562 * @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.
1563 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
1564 * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible.
1565 * @param[out] enums Newly created array of the compiled enums information for the current type.
1566 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1567 */
Radek Krejci19a96102018-11-15 13:38:09 +01001568static LY_ERR
1569lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype, int options,
1570 struct lysc_type_enum_item *base_enums, struct lysc_type_enum_item **enums)
1571{
1572 LY_ERR ret = LY_SUCCESS;
1573 unsigned int u, v, match;
1574 int32_t value = 0;
1575 uint32_t position = 0;
1576 struct lysc_type_enum_item *e, storage;
1577
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001578 if (base_enums && ctx->mod_def->version < 2) {
Radek Krejci19a96102018-11-15 13:38:09 +01001579 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
1580 basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
1581 return LY_EVALID;
1582 }
1583
1584 LY_ARRAY_FOR(enums_p, u) {
1585 LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM);
1586 DUP_STRING(ctx->ctx, enums_p[u].name, e->name);
Radek Krejcic8b31002019-01-08 10:24:45 +01001587 DUP_STRING(ctx->ctx, enums_p[u].ref, e->dsc);
1588 DUP_STRING(ctx->ctx, enums_p[u].ref, e->ref);
Radek Krejci19a96102018-11-15 13:38:09 +01001589 if (base_enums) {
1590 /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
1591 LY_ARRAY_FOR(base_enums, v) {
1592 if (!strcmp(e->name, base_enums[v].name)) {
1593 break;
1594 }
1595 }
1596 if (v == LY_ARRAY_SIZE(base_enums)) {
1597 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1598 "Invalid %s - derived type adds new item \"%s\".",
1599 basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name);
1600 return LY_EVALID;
1601 }
1602 match = v;
1603 }
1604
1605 if (basetype == LY_TYPE_ENUM) {
1606 if (enums_p[u].flags & LYS_SET_VALUE) {
1607 e->value = (int32_t)enums_p[u].value;
1608 if (!u || e->value >= value) {
1609 value = e->value + 1;
1610 }
1611 /* check collision with other values */
1612 for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) {
1613 if (e->value == (*enums)[v].value) {
1614 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1615 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
1616 e->value, e->name, (*enums)[v].name);
1617 return LY_EVALID;
1618 }
1619 }
1620 } else if (base_enums) {
1621 /* inherit the assigned value */
1622 e->value = base_enums[match].value;
1623 if (!u || e->value >= value) {
1624 value = e->value + 1;
1625 }
1626 } else {
1627 /* assign value automatically */
1628 if (u && value == INT32_MIN) {
1629 /* counter overflow */
1630 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1631 "Invalid enumeration - it is not possible to auto-assign enum value for "
1632 "\"%s\" since the highest value is already 2147483647.", e->name);
1633 return LY_EVALID;
1634 }
1635 e->value = value++;
1636 }
1637 } else { /* LY_TYPE_BITS */
1638 if (enums_p[u].flags & LYS_SET_VALUE) {
1639 e->value = (int32_t)enums_p[u].value;
1640 if (!u || (uint32_t)e->value >= position) {
1641 position = (uint32_t)e->value + 1;
1642 }
1643 /* check collision with other values */
1644 for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) {
1645 if (e->value == (*enums)[v].value) {
1646 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1647 "Invalid bits - position %u collide in items \"%s\" and \"%s\".",
1648 (uint32_t)e->value, e->name, (*enums)[v].name);
1649 return LY_EVALID;
1650 }
1651 }
1652 } else if (base_enums) {
1653 /* inherit the assigned value */
1654 e->value = base_enums[match].value;
1655 if (!u || (uint32_t)e->value >= position) {
1656 position = (uint32_t)e->value + 1;
1657 }
1658 } else {
1659 /* assign value automatically */
1660 if (u && position == 0) {
1661 /* counter overflow */
1662 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1663 "Invalid bits - it is not possible to auto-assign bit position for "
1664 "\"%s\" since the highest value is already 4294967295.", e->name);
1665 return LY_EVALID;
1666 }
1667 e->value = position++;
1668 }
1669 }
1670
1671 if (base_enums) {
1672 /* the assigned values must not change from the derived type */
1673 if (e->value != base_enums[match].value) {
1674 if (basetype == LY_TYPE_ENUM) {
1675 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1676 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
1677 e->name, base_enums[match].value, e->value);
1678 } else {
1679 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1680 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
1681 e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value);
1682 }
1683 return LY_EVALID;
1684 }
1685 }
1686
1687 COMPILE_ARRAY_GOTO(ctx, enums_p[u].iffeatures, e->iffeatures, options, v, lys_compile_iffeature, ret, done);
1688 COMPILE_ARRAY_GOTO(ctx, enums_p[u].exts, e->exts, options, v, lys_compile_ext, ret, done);
1689
1690 if (basetype == LY_TYPE_BITS) {
1691 /* keep bits ordered by position */
1692 for (v = u; v && (*enums)[v - 1].value > e->value; --v);
1693 if (v != u) {
1694 memcpy(&storage, e, sizeof *e);
1695 memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums);
1696 memcpy(&(*enums)[v], &storage, sizeof storage);
1697 }
1698 }
1699 }
1700
1701done:
1702 return ret;
1703}
1704
Radek Krejcia3045382018-11-22 14:30:31 +01001705#define MOVE_PATH_PARENT(NODE, LIMIT_COND, TERM, ERR_MSG, ...) \
1706 for ((NODE) = (NODE)->parent; \
1707 (NODE) && !((NODE)->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_ACTION | LYS_NOTIF | LYS_ACTION)); \
1708 (NODE) = (NODE)->parent); \
1709 if (!(NODE) && (LIMIT_COND)) { /* we are going higher than top-level */ \
1710 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, ERR_MSG, ##__VA_ARGS__); \
1711 TERM; \
1712 }
1713
1714/**
1715 * @brief Validate the predicate(s) from the leafref path.
1716 * @param[in] ctx Compile context
1717 * @param[in, out] predicate Pointer to the predicate in the leafref path. The pointer is moved after the validated predicate(s).
1718 * Since there can be multiple adjacent predicates for lists with multiple keys, all such predicates are validated.
Radek Krejci4ce68932018-11-28 12:53:36 +01001719 * @param[in] start_node Path context node (where the path is instantiated).
Radek Krejcia3045382018-11-22 14:30:31 +01001720 * @param[in] context_node Predicate context node (where the predicate is placed).
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001721 * @param[in] path_context Schema where the path was defined to correct resolve of the prefixes.
Radek Krejcia3045382018-11-22 14:30:31 +01001722 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1723 */
1724static LY_ERR
Radek Krejci4ce68932018-11-28 12:53:36 +01001725lys_compile_leafref_predicate_validate(struct lysc_ctx *ctx, const char **predicate, const struct lysc_node *start_node,
Radek Krejcibade82a2018-12-05 10:13:48 +01001726 const struct lysc_node_list *context_node, const struct lys_module *path_context)
Radek Krejcia3045382018-11-22 14:30:31 +01001727{
1728 LY_ERR ret = LY_EVALID;
1729 const struct lys_module *mod;
1730 const struct lysc_node *src_node, *dst_node;
1731 const char *path_key_expr, *pke_start, *src, *src_prefix, *dst, *dst_prefix;
1732 size_t src_len, src_prefix_len, dst_len, dst_prefix_len;
Radek Krejcibade82a2018-12-05 10:13:48 +01001733 unsigned int dest_parent_times, c, u;
Radek Krejcia3045382018-11-22 14:30:31 +01001734 const char *start, *end, *pke_end;
1735 struct ly_set keys = {0};
1736 int i;
1737
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001738 assert(path_context);
1739
Radek Krejcia3045382018-11-22 14:30:31 +01001740 while (**predicate == '[') {
1741 start = (*predicate)++;
1742
1743 while (isspace(**predicate)) {
1744 ++(*predicate);
1745 }
1746 LY_CHECK_GOTO(lys_parse_nodeid(predicate, &src_prefix, &src_prefix_len, &src, &src_len), cleanup);
1747 while (isspace(**predicate)) {
1748 ++(*predicate);
1749 }
1750 if (**predicate != '=') {
1751 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01001752 "Invalid leafref path predicate \"%.*s\" - missing \"=\" after node-identifier.",
1753 *predicate - start + 1, start);
Radek Krejcia3045382018-11-22 14:30:31 +01001754 goto cleanup;
1755 }
1756 ++(*predicate);
1757 while (isspace(**predicate)) {
1758 ++(*predicate);
1759 }
1760
1761 if ((end = pke_end = strchr(*predicate, ']')) == NULL) {
1762 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1763 "Invalid leafref path predicate \"%s\" - missing predicate termination.", start);
1764 goto cleanup;
1765 }
1766 --pke_end;
1767 while (isspace(*pke_end)) {
1768 --pke_end;
1769 }
Radek Krejci7adf4ff2018-12-05 08:55:00 +01001770 ++pke_end;
Radek Krejcia3045382018-11-22 14:30:31 +01001771 /* localize path-key-expr */
1772 pke_start = path_key_expr = *predicate;
1773 /* move after the current predicate */
1774 *predicate = end + 1;
1775
1776 /* source (must be leaf or leaf-list) */
1777 if (src_prefix) {
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001778 mod = lys_module_find_prefix(path_context, src_prefix, src_prefix_len);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001779 if (!mod) {
1780 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1781 "Invalid leafref path predicate \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".",
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001782 *predicate - start, start, src_prefix_len, src_prefix, path_context->name);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001783 goto cleanup;
1784 }
Radek Krejcia3045382018-11-22 14:30:31 +01001785 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01001786 mod = start_node->module;
Radek Krejcia3045382018-11-22 14:30:31 +01001787 }
Radek Krejcibade82a2018-12-05 10:13:48 +01001788 src_node = NULL;
1789 if (context_node->keys) {
1790 for (u = 0; u < LY_ARRAY_SIZE(context_node->keys); ++u) {
1791 if (!strncmp(src, context_node->keys[u]->name, src_len) && context_node->keys[u]->name[src_len] == '\0') {
1792 src_node = (const struct lysc_node*)context_node->keys[u];
1793 break;
1794 }
1795 }
1796 }
Radek Krejcia3045382018-11-22 14:30:31 +01001797 if (!src_node) {
1798 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01001799 "Invalid leafref path predicate \"%.*s\" - predicate's key node \"%.*s\" not found.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001800 *predicate - start, start, src_len, src, mod->name);
Radek Krejcia3045382018-11-22 14:30:31 +01001801 goto cleanup;
1802 }
Radek Krejcia3045382018-11-22 14:30:31 +01001803
1804 /* check that there is only one predicate for the */
Radek Krejcibade82a2018-12-05 10:13:48 +01001805 c = keys.count;
Radek Krejcia3045382018-11-22 14:30:31 +01001806 i = ly_set_add(&keys, (void*)src_node, 0);
1807 LY_CHECK_GOTO(i == -1, cleanup);
Radek Krejcibade82a2018-12-05 10:13:48 +01001808 if (keys.count == c) { /* node was already present in the set */
Radek Krejcia3045382018-11-22 14:30:31 +01001809 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01001810 "Invalid leafref path predicate \"%.*s\" - multiple equality tests for the key \"%s\".",
Radek Krejcia3045382018-11-22 14:30:31 +01001811 *predicate - start, start, src_node->name);
1812 goto cleanup;
1813 }
1814
1815 /* destination */
1816 dest_parent_times = 0;
Radek Krejci4ce68932018-11-28 12:53:36 +01001817 dst_node = start_node;
Radek Krejcia3045382018-11-22 14:30:31 +01001818
1819 /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */
1820 if (strncmp(path_key_expr, "current()", 9)) {
1821 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1822 "Invalid leafref path predicate \"%.*s\" - missing current-function-invocation.",
1823 *predicate - start, start);
1824 goto cleanup;
1825 }
1826 path_key_expr += 9;
1827 while (isspace(*path_key_expr)) {
1828 ++path_key_expr;
1829 }
1830
1831 if (*path_key_expr != '/') {
1832 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1833 "Invalid leafref path predicate \"%.*s\" - missing \"/\" after current-function-invocation.",
1834 *predicate - start, start);
1835 goto cleanup;
1836 }
1837 ++path_key_expr;
1838 while (isspace(*path_key_expr)) {
1839 ++path_key_expr;
1840 }
1841
1842 /* rel-path-keyexpr:
1843 * 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier */
1844 while (!strncmp(path_key_expr, "..", 2)) {
1845 ++dest_parent_times;
1846 path_key_expr += 2;
1847 while (isspace(*path_key_expr)) {
1848 ++path_key_expr;
1849 }
1850 if (*path_key_expr != '/') {
1851 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1852 "Invalid leafref path predicate \"%.*s\" - missing \"/\" in \"../\" rel-path-keyexpr pattern.",
1853 *predicate - start, start);
1854 goto cleanup;
1855 }
1856 ++path_key_expr;
1857 while (isspace(*path_key_expr)) {
1858 ++path_key_expr;
1859 }
1860
1861 /* path is supposed to be evaluated in data tree, so we have to skip
1862 * all schema nodes that cannot be instantiated in data tree */
1863 MOVE_PATH_PARENT(dst_node, !strncmp(path_key_expr, "..", 2), goto cleanup,
1864 "Invalid leafref path predicate \"%.*s\" - too many \"..\" in rel-path-keyexpr.",
1865 *predicate - start, start);
1866 }
1867 if (!dest_parent_times) {
1868 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1869 "Invalid leafref path predicate \"%.*s\" - at least one \"..\" is expected in rel-path-keyexpr.",
1870 *predicate - start, start);
1871 goto cleanup;
1872 }
1873 if (path_key_expr == pke_end) {
1874 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1875 "Invalid leafref path predicate \"%.*s\" - at least one node-identifier is expected in rel-path-keyexpr.",
1876 *predicate - start, start);
1877 goto cleanup;
1878 }
1879
1880 while(path_key_expr != pke_end) {
1881 if (lys_parse_nodeid(&path_key_expr, &dst_prefix, &dst_prefix_len, &dst, &dst_len)) {
1882 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcibade82a2018-12-05 10:13:48 +01001883 "Invalid node identifier in leafref path predicate - character %d (of %.*s).",
1884 path_key_expr - start + 1, *predicate - start, start);
Radek Krejcia3045382018-11-22 14:30:31 +01001885 goto cleanup;
1886 }
1887
1888 if (dst_prefix) {
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001889 mod = lys_module_find_prefix(path_context, dst_prefix, dst_prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01001890 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01001891 mod = start_node->module;
Radek Krejcia3045382018-11-22 14:30:31 +01001892 }
1893 if (!mod) {
1894 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1895 "Invalid leafref path predicate \"%.*s\" - unable to find module of the node \"%.*s\" in rel-path_keyexpr.",
1896 *predicate - start, start, dst_len, dst);
1897 goto cleanup;
1898 }
1899
1900 dst_node = lys_child(dst_node, mod, dst, dst_len, 0, LYS_GETNEXT_NOSTATECHECK);
1901 if (!dst_node) {
1902 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1903 "Invalid leafref path predicate \"%.*s\" - unable to find node \"%.*s\" in the rel-path_keyexpr.",
1904 *predicate - start, start, path_key_expr - pke_start, pke_start);
1905 goto cleanup;
1906 }
1907 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001908 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 +01001909 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01001910 "Invalid leafref path predicate \"%.*s\" - rel-path_keyexpr \"%.*s\" refers %s instead of leaf.",
Radek Krejcia3045382018-11-22 14:30:31 +01001911 *predicate - start, start, path_key_expr - pke_start, pke_start, lys_nodetype2str(dst_node->nodetype));
1912 goto cleanup;
1913 }
1914 }
1915
1916 ret = LY_SUCCESS;
1917cleanup:
1918 ly_set_erase(&keys, NULL);
1919 return ret;
1920}
1921
1922/**
1923 * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function.
1924 *
1925 * path-arg = absolute-path / relative-path
1926 * absolute-path = 1*("/" (node-identifier *path-predicate))
1927 * relative-path = 1*(".." "/") descendant-path
1928 *
1929 * @param[in,out] path Path to parse.
1930 * @param[out] prefix Prefix of the token, NULL if there is not any.
1931 * @param[out] pref_len Length of the prefix, 0 if there is not any.
1932 * @param[out] name Name of the token.
1933 * @param[out] nam_len Length of the name.
1934 * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call,
1935 * must not be changed between consecutive calls. -1 if the
1936 * path is absolute.
1937 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
1938 * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path.
1939 */
1940static LY_ERR
1941lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len,
1942 int *parent_times, int *has_predicate)
1943{
1944 int par_times = 0;
1945
1946 assert(path && *path);
1947 assert(parent_times);
1948 assert(prefix);
1949 assert(prefix_len);
1950 assert(name);
1951 assert(name_len);
1952 assert(has_predicate);
1953
1954 *prefix = NULL;
1955 *prefix_len = 0;
1956 *name = NULL;
1957 *name_len = 0;
1958 *has_predicate = 0;
1959
1960 if (!*parent_times) {
1961 if (!strncmp(*path, "..", 2)) {
1962 *path += 2;
1963 ++par_times;
1964 while (!strncmp(*path, "/..", 3)) {
1965 *path += 3;
1966 ++par_times;
1967 }
1968 }
1969 if (par_times) {
1970 *parent_times = par_times;
1971 } else {
1972 *parent_times = -1;
1973 }
1974 }
1975
1976 if (**path != '/') {
1977 return LY_EINVAL;
1978 }
1979 /* skip '/' */
1980 ++(*path);
1981
1982 /* node-identifier ([prefix:]name) */
1983 LY_CHECK_RET(lys_parse_nodeid(path, prefix, prefix_len, name, name_len));
1984
1985 if ((**path == '/' && (*path)[1]) || !**path) {
1986 /* path continues by another token or this is the last token */
1987 return LY_SUCCESS;
1988 } else if ((*path)[0] != '[') {
1989 /* unexpected character */
1990 return LY_EINVAL;
1991 } else {
1992 /* predicate starting with [ */
1993 *has_predicate = 1;
1994 return LY_SUCCESS;
1995 }
1996}
1997
1998/**
Radek Krejci58d171e2018-11-23 13:50:55 +01001999 * @brief Check the features used in if-feature statements applicable to the leafref and its target.
2000 *
2001 * The set of features used for target must be a subset of features used for the leafref.
2002 * This is not a perfect, we should compare the truth tables but it could require too much resources
2003 * and RFC 7950 does not require it explicitely, so we simplify that.
2004 *
2005 * @param[in] refnode The leafref node.
2006 * @param[in] target Tha target node of the leafref.
2007 * @return LY_SUCCESS or LY_EVALID;
2008 */
2009static LY_ERR
2010lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target)
2011{
2012 LY_ERR ret = LY_EVALID;
2013 const struct lysc_node *iter;
Radek Krejci58d171e2018-11-23 13:50:55 +01002014 unsigned int u, v, count;
2015 struct ly_set features = {0};
2016
2017 for (iter = refnode; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002018 if (iter->iffeatures) {
2019 LY_ARRAY_FOR(iter->iffeatures, u) {
2020 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2021 LY_CHECK_GOTO(ly_set_add(&features, iter->iffeatures[u].features[v], 0) == -1, cleanup);
Radek Krejci58d171e2018-11-23 13:50:55 +01002022 }
2023 }
2024 }
2025 }
2026
2027 /* we should have, in features set, a superset of features applicable to the target node.
2028 * So when adding features applicable to the target into the features set, we should not be
2029 * able to actually add any new feature, otherwise it is not a subset of features applicable
2030 * to the leafref itself. */
2031 count = features.count;
2032 for (iter = target; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002033 if (iter->iffeatures) {
2034 LY_ARRAY_FOR(iter->iffeatures, u) {
2035 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2036 if ((unsigned int)ly_set_add(&features, iter->iffeatures[u].features[v], 0) >= count) {
Radek Krejci58d171e2018-11-23 13:50:55 +01002037 /* new feature was added (or LY_EMEM) */
2038 goto cleanup;
2039 }
2040 }
2041 }
2042 }
2043 }
2044 ret = LY_SUCCESS;
2045
2046cleanup:
2047 ly_set_erase(&features, NULL);
2048 return ret;
2049}
2050
2051/**
Radek Krejcia3045382018-11-22 14:30:31 +01002052 * @brief Validate the leafref path.
2053 * @param[in] ctx Compile context
2054 * @param[in] startnode Path context node (where the leafref path begins/is placed).
Radek Krejci412ddfa2018-11-23 11:44:11 +01002055 * @param[in] leafref Leafref to validate.
Radek Krejcia3045382018-11-22 14:30:31 +01002056 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2057 */
2058static LY_ERR
Radek Krejci412ddfa2018-11-23 11:44:11 +01002059lys_compile_leafref_validate(struct lysc_ctx *ctx, struct lysc_node *startnode, struct lysc_type_leafref *leafref)
Radek Krejcia3045382018-11-22 14:30:31 +01002060{
2061 const struct lysc_node *node = NULL, *parent = NULL;
2062 const struct lys_module *mod;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002063 struct lysc_type *type;
Radek Krejcia3045382018-11-22 14:30:31 +01002064 const char *id, *prefix, *name;
2065 size_t prefix_len, name_len;
2066 int parent_times = 0, has_predicate;
2067 unsigned int iter, u;
2068 LY_ERR ret = LY_SUCCESS;
2069
2070 assert(ctx);
2071 assert(startnode);
Radek Krejci412ddfa2018-11-23 11:44:11 +01002072 assert(leafref);
2073
2074 /* 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 +01002075
2076 iter = 0;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002077 id = leafref->path;
Radek Krejcia3045382018-11-22 14:30:31 +01002078 while(*id && (ret = lys_path_token(&id, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)) == LY_SUCCESS) {
2079 if (!iter) { /* first iteration */
2080 /* precess ".." in relative paths */
2081 if (parent_times > 0) {
2082 /* move from the context node */
2083 for (u = 0, parent = startnode; u < (unsigned int)parent_times; u++) {
2084 /* path is supposed to be evaluated in data tree, so we have to skip
2085 * all schema nodes that cannot be instantiated in data tree */
2086 MOVE_PATH_PARENT(parent, u < (unsigned int)parent_times - 1, return LY_EVALID,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002087 "Invalid leafref path \"%s\" - too many \"..\" in the path.", leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002088 }
2089 }
2090 }
2091
2092 if (prefix) {
Radek Krejci412ddfa2018-11-23 11:44:11 +01002093 mod = lys_module_find_prefix(leafref->path_context, prefix, prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01002094 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002095 mod = startnode->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002096 }
2097 if (!mod) {
2098 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002099 "Invalid leafref path - unable to find module connected with the prefix of the node \"%.*s\".",
2100 id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002101 return LY_EVALID;
2102 }
2103
2104 node = lys_child(parent, mod, name, name_len, 0, LYS_GETNEXT_NOSTATECHECK);
2105 if (!node) {
2106 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002107 "Invalid leafref path - unable to find \"%.*s\".", id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002108 return LY_EVALID;
2109 }
2110 parent = node;
2111
2112 if (has_predicate) {
2113 /* we have predicate, so the current result must be list */
2114 if (node->nodetype != LYS_LIST) {
2115 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2116 "Invalid leafref path - node \"%.*s\" is expected to be a list, but it is %s.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01002117 id - leafref->path, leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01002118 return LY_EVALID;
2119 }
2120
Radek Krejcibade82a2018-12-05 10:13:48 +01002121 LY_CHECK_RET(lys_compile_leafref_predicate_validate(ctx, &id, startnode, (struct lysc_node_list*)node, leafref->path_context),
2122 LY_EVALID);
Radek Krejcia3045382018-11-22 14:30:31 +01002123 }
2124
2125 ++iter;
2126 }
2127 if (ret) {
2128 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002129 "Invalid leafref path at character %d (%s).", id - leafref->path + 1, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002130 return LY_EVALID;
2131 }
2132
2133 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
2134 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2135 "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01002136 leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01002137 return LY_EVALID;
2138 }
2139
2140 /* check status */
2141 if (lysc_check_status(ctx, startnode->flags, startnode->module, startnode->name, node->flags, node->module, node->name)) {
2142 return LY_EVALID;
2143 }
2144
Radek Krejcib57cf1e2018-11-23 14:07:05 +01002145 /* check config */
2146 if (leafref->require_instance && (startnode->flags & LYS_CONFIG_W)) {
2147 if (node->flags & LYS_CONFIG_R) {
2148 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2149 "Invalid leafref path \"%s\" - target is supposed to represent configuration data (as the leafref does), but it does not.",
2150 leafref->path);
2151 return LY_EVALID;
2152 }
2153 }
2154
Radek Krejci412ddfa2018-11-23 11:44:11 +01002155 /* store the target's type and check for circular chain of leafrefs */
2156 leafref->realtype = ((struct lysc_node_leaf*)node)->type;
2157 for (type = leafref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref*)type)->realtype) {
2158 if (type == (struct lysc_type*)leafref) {
2159 /* circular chain detected */
2160 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2161 "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", leafref->path);
2162 return LY_EVALID;
2163 }
2164 }
2165
Radek Krejci58d171e2018-11-23 13:50:55 +01002166 /* check if leafref and its target are under common if-features */
2167 if (lys_compile_leafref_features_validate(startnode, node)) {
2168 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2169 "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of features applicable to the leafref itself.",
2170 leafref->path);
2171 return LY_EVALID;
2172 }
2173
Radek Krejcia3045382018-11-22 14:30:31 +01002174 return LY_SUCCESS;
2175}
2176
Radek Krejcicdfecd92018-11-26 11:27:32 +01002177static 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 +01002178 struct lysp_type *type_p, int options, struct lysc_type **type, const char **units);
Radek Krejcia3045382018-11-22 14:30:31 +01002179/**
2180 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
2181 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002182 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2183 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2184 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2185 * @param[in] context_name Name of the context node or referencing typedef for logging.
Radek Krejcia3045382018-11-22 14:30:31 +01002186 * @param[in] type_p Parsed type to compile.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002187 * @param[in] module Context module for the leafref path (to correctly resolve prefixes in path)
Radek Krejcia3045382018-11-22 14:30:31 +01002188 * @param[in] basetype Base YANG built-in type of the type to compile.
2189 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
2190 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
2191 * @param[in] base The latest base (compiled) type from which the current type is being derived.
2192 * @param[out] type Newly created type structure with the filled information about the type.
2193 * @return LY_ERR value.
2194 */
Radek Krejci19a96102018-11-15 13:38:09 +01002195static LY_ERR
Radek Krejcicdfecd92018-11-26 11:27:32 +01002196lys_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,
2197 struct lysp_type *type_p, struct lys_module *module, LY_DATA_TYPE basetype, int options, const char *tpdfname,
2198 struct lysc_type *base, struct lysc_type **type)
Radek Krejcic5c27e52018-11-15 14:38:11 +01002199{
2200 LY_ERR ret = LY_SUCCESS;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002201 unsigned int u, v, additional;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002202 struct lysc_type_bin *bin;
2203 struct lysc_type_num *num;
2204 struct lysc_type_str *str;
2205 struct lysc_type_bits *bits;
2206 struct lysc_type_enum *enumeration;
Radek Krejci6cba4292018-11-15 17:33:29 +01002207 struct lysc_type_dec *dec;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002208 struct lysc_type_identityref *idref;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002209 struct lysc_type_union *un, *un_aux;
2210 void *p;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002211
2212 switch (basetype) {
2213 case LY_TYPE_BINARY:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002214 bin = (struct lysc_type_bin*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002215
2216 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002217 if (type_p->length) {
Radek Krejci6cba4292018-11-15 17:33:29 +01002218 ret = lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
Radek Krejcic5c27e52018-11-15 14:38:11 +01002219 base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length);
2220 LY_CHECK_RET(ret);
2221 if (!tpdfname) {
2222 COMPILE_ARRAY_GOTO(ctx, type_p->length->exts, bin->length->exts,
2223 options, u, lys_compile_ext, ret, done);
2224 }
2225 }
2226
2227 if (tpdfname) {
2228 type_p->compiled = *type;
2229 *type = calloc(1, sizeof(struct lysc_type_bin));
2230 }
2231 break;
2232 case LY_TYPE_BITS:
2233 /* RFC 7950 9.7 - bits */
2234 bits = (struct lysc_type_bits*)(*type);
2235 if (type_p->bits) {
2236 ret = lys_compile_type_enums(ctx, type_p->bits, basetype, options,
2237 base ? (struct lysc_type_enum_item*)((struct lysc_type_bits*)base)->bits : NULL,
2238 (struct lysc_type_enum_item**)&bits->bits);
2239 LY_CHECK_RET(ret);
2240 }
2241
Radek Krejci555cb5b2018-11-16 14:54:33 +01002242 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002243 /* type derived from bits built-in type must contain at least one bit */
Radek Krejci6cba4292018-11-15 17:33:29 +01002244 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002245 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002246 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002247 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002248 free(*type);
2249 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002250 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002251 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002252 }
2253
2254 if (tpdfname) {
2255 type_p->compiled = *type;
2256 *type = calloc(1, sizeof(struct lysc_type_bits));
2257 }
2258 break;
Radek Krejci6cba4292018-11-15 17:33:29 +01002259 case LY_TYPE_DEC64:
2260 dec = (struct lysc_type_dec*)(*type);
2261
2262 /* RFC 7950 9.3.4 - fraction-digits */
Radek Krejci555cb5b2018-11-16 14:54:33 +01002263 if (!base) {
Radek Krejci643c8242018-11-15 17:51:11 +01002264 if (!type_p->fraction_digits) {
2265 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002266 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Radek Krejci643c8242018-11-15 17:51:11 +01002267 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002268 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Radek Krejci643c8242018-11-15 17:51:11 +01002269 free(*type);
2270 *type = NULL;
2271 }
2272 return LY_EVALID;
2273 }
2274 } else if (type_p->fraction_digits) {
2275 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
Radek Krejci6cba4292018-11-15 17:33:29 +01002276 if (tpdfname) {
Radek Krejci643c8242018-11-15 17:51:11 +01002277 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2278 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
Radek Krejci6cba4292018-11-15 17:33:29 +01002279 tpdfname);
2280 } else {
Radek Krejci643c8242018-11-15 17:51:11 +01002281 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2282 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
Radek Krejci6cba4292018-11-15 17:33:29 +01002283 free(*type);
2284 *type = NULL;
2285 }
2286 return LY_EVALID;
2287 }
2288 dec->fraction_digits = type_p->fraction_digits;
2289
2290 /* RFC 7950 9.2.4 - range */
2291 if (type_p->range) {
2292 ret = lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
2293 base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range);
2294 LY_CHECK_RET(ret);
2295 if (!tpdfname) {
2296 COMPILE_ARRAY_GOTO(ctx, type_p->range->exts, dec->range->exts,
2297 options, u, lys_compile_ext, ret, done);
2298 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002299 }
2300
2301 if (tpdfname) {
2302 type_p->compiled = *type;
2303 *type = calloc(1, sizeof(struct lysc_type_dec));
2304 }
2305 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002306 case LY_TYPE_STRING:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002307 str = (struct lysc_type_str*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002308
2309 /* RFC 7950 9.4.4 - length */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002310 if (type_p->length) {
Radek Krejci6cba4292018-11-15 17:33:29 +01002311 ret = lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
Radek Krejcic5c27e52018-11-15 14:38:11 +01002312 base ? ((struct lysc_type_str*)base)->length : NULL, &str->length);
2313 LY_CHECK_RET(ret);
2314 if (!tpdfname) {
2315 COMPILE_ARRAY_GOTO(ctx, type_p->length->exts, str->length->exts,
2316 options, u, lys_compile_ext, ret, done);
2317 }
2318 } else if (base && ((struct lysc_type_str*)base)->length) {
2319 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length);
2320 }
2321
2322 /* RFC 7950 9.4.5 - pattern */
2323 if (type_p->patterns) {
2324 ret = lys_compile_type_patterns(ctx, type_p->patterns, options,
2325 base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns);
2326 LY_CHECK_RET(ret);
2327 } else if (base && ((struct lysc_type_str*)base)->patterns) {
2328 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns);
2329 }
2330
2331 if (tpdfname) {
2332 type_p->compiled = *type;
2333 *type = calloc(1, sizeof(struct lysc_type_str));
2334 }
2335 break;
2336 case LY_TYPE_ENUM:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002337 enumeration = (struct lysc_type_enum*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002338
2339 /* RFC 7950 9.6 - enum */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002340 if (type_p->enums) {
2341 ret = lys_compile_type_enums(ctx, type_p->enums, basetype, options,
2342 base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums);
2343 LY_CHECK_RET(ret);
2344 }
2345
Radek Krejci555cb5b2018-11-16 14:54:33 +01002346 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002347 /* type derived from enumerations built-in type must contain at least one enum */
Radek Krejci6cba4292018-11-15 17:33:29 +01002348 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002349 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002350 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002351 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002352 free(*type);
2353 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002354 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002355 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002356 }
2357
2358 if (tpdfname) {
2359 type_p->compiled = *type;
2360 *type = calloc(1, sizeof(struct lysc_type_enum));
2361 }
2362 break;
2363 case LY_TYPE_INT8:
2364 case LY_TYPE_UINT8:
2365 case LY_TYPE_INT16:
2366 case LY_TYPE_UINT16:
2367 case LY_TYPE_INT32:
2368 case LY_TYPE_UINT32:
2369 case LY_TYPE_INT64:
2370 case LY_TYPE_UINT64:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002371 num = (struct lysc_type_num*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002372
2373 /* RFC 6020 9.2.4 - range */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002374 if (type_p->range) {
Radek Krejci6cba4292018-11-15 17:33:29 +01002375 ret = lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
Radek Krejcic5c27e52018-11-15 14:38:11 +01002376 base ? ((struct lysc_type_num*)base)->range : NULL, &num->range);
2377 LY_CHECK_RET(ret);
2378 if (!tpdfname) {
2379 COMPILE_ARRAY_GOTO(ctx, type_p->range->exts, num->range->exts,
2380 options, u, lys_compile_ext, ret, done);
2381 }
2382 }
2383
2384 if (tpdfname) {
2385 type_p->compiled = *type;
2386 *type = calloc(1, sizeof(struct lysc_type_num));
2387 }
2388 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002389 case LY_TYPE_IDENT:
2390 idref = (struct lysc_type_identityref*)(*type);
2391
2392 /* RFC 7950 9.10.2 - base */
2393 if (type_p->bases) {
2394 if (base) {
2395 /* only the directly derived identityrefs can contain base specification */
2396 if (tpdfname) {
2397 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002398 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
Radek Krejci555cb5b2018-11-16 14:54:33 +01002399 tpdfname);
2400 } else {
2401 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002402 "Invalid base substatement for the type not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01002403 free(*type);
2404 *type = NULL;
2405 }
2406 return LY_EVALID;
2407 }
2408 ret = lys_compile_identity_bases(ctx, type_p->bases, NULL, &idref->bases);
2409 LY_CHECK_RET(ret);
2410 }
2411
2412 if (!base && !type_p->flags) {
2413 /* type derived from identityref built-in type must contain at least one base */
2414 if (tpdfname) {
2415 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
2416 } else {
2417 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
2418 free(*type);
2419 *type = NULL;
2420 }
2421 return LY_EVALID;
2422 }
2423
2424 if (tpdfname) {
2425 type_p->compiled = *type;
2426 *type = calloc(1, sizeof(struct lysc_type_identityref));
2427 }
2428 break;
Radek Krejcia3045382018-11-22 14:30:31 +01002429 case LY_TYPE_LEAFREF:
2430 /* RFC 7950 9.9.3 - require-instance */
2431 if (type_p->flags & LYS_SET_REQINST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002432 if (context_mod->mod->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002433 if (tpdfname) {
2434 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2435 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
2436 } else {
2437 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2438 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
2439 free(*type);
2440 *type = NULL;
2441 }
2442 return LY_EVALID;
2443 }
Radek Krejcia3045382018-11-22 14:30:31 +01002444 ((struct lysc_type_leafref*)(*type))->require_instance = type_p->require_instance;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002445 } else if (base) {
2446 /* inherit */
2447 ((struct lysc_type_leafref*)(*type))->require_instance = ((struct lysc_type_leafref*)base)->require_instance;
Radek Krejcia3045382018-11-22 14:30:31 +01002448 } else {
2449 /* default is true */
2450 ((struct lysc_type_leafref*)(*type))->require_instance = 1;
2451 }
2452 if (type_p->path) {
2453 DUP_STRING(ctx->ctx, (void*)type_p->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002454 ((struct lysc_type_leafref*)(*type))->path_context = module;
Radek Krejcia3045382018-11-22 14:30:31 +01002455 } else if (base) {
2456 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)base)->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002457 ((struct lysc_type_leafref*)(*type))->path_context = ((struct lysc_type_leafref*)base)->path_context;
Radek Krejcia3045382018-11-22 14:30:31 +01002458 } else if (tpdfname) {
2459 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
2460 return LY_EVALID;
2461 } else {
2462 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
2463 free(*type);
2464 *type = NULL;
2465 return LY_EVALID;
2466 }
2467 if (tpdfname) {
2468 type_p->compiled = *type;
2469 *type = calloc(1, sizeof(struct lysc_type_leafref));
2470 }
2471 break;
Radek Krejci16c0f822018-11-16 10:46:10 +01002472 case LY_TYPE_INST:
2473 /* RFC 7950 9.9.3 - require-instance */
2474 if (type_p->flags & LYS_SET_REQINST) {
2475 ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance;
2476 } else {
2477 /* default is true */
2478 ((struct lysc_type_instanceid*)(*type))->require_instance = 1;
2479 }
2480
2481 if (tpdfname) {
2482 type_p->compiled = *type;
2483 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2484 }
2485 break;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002486 case LY_TYPE_UNION:
2487 un = (struct lysc_type_union*)(*type);
2488
2489 /* RFC 7950 7.4 - type */
2490 if (type_p->types) {
2491 if (base) {
2492 /* only the directly derived union can contain types specification */
2493 if (tpdfname) {
2494 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2495 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
2496 tpdfname);
2497 } else {
2498 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2499 "Invalid type substatement for the type not directly derived from union built-in type.");
2500 free(*type);
2501 *type = NULL;
2502 }
2503 return LY_EVALID;
2504 }
2505 /* compile the type */
2506 additional = 0;
2507 LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_SIZE(type_p->types), LY_EVALID);
2508 for (u = 0; u < LY_ARRAY_SIZE(type_p->types); ++u) {
Radek Krejci01342af2019-01-03 15:18:08 +01002509 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 +01002510 if (un->types[u + additional]->basetype == LY_TYPE_UNION) {
2511 /* add space for additional types from the union subtype */
2512 un_aux = (struct lysc_type_union *)un->types[u + additional];
2513 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)));
2514 LY_CHECK_ERR_RET(!p, LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
2515 un->types = (void*)((uint32_t*)(p) + 1);
2516
2517 /* copy subtypes of the subtype union */
2518 for (v = 0; v < LY_ARRAY_SIZE(un_aux->types); ++v) {
2519 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
2520 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
2521 un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
2522 LY_CHECK_ERR_RET(!un->types[u + additional], LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
2523 ((struct lysc_type_leafref*)un->types[u + additional])->basetype = LY_TYPE_LEAFREF;
2524 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)un_aux->types[v])->path, ((struct lysc_type_leafref*)un->types[u + additional])->path);
2525 ((struct lysc_type_leafref*)un->types[u + additional])->refcount = 1;
2526 ((struct lysc_type_leafref*)un->types[u + additional])->require_instance = ((struct lysc_type_leafref*)un_aux->types[v])->require_instance;
2527 ((struct lysc_type_leafref*)un->types[u + additional])->path_context = ((struct lysc_type_leafref*)un_aux->types[v])->path_context;
2528 /* TODO extensions */
2529
2530 } else {
2531 un->types[u + additional] = un_aux->types[v];
2532 ++un_aux->types[v]->refcount;
2533 }
2534 ++additional;
2535 LY_ARRAY_INCREMENT(un->types);
2536 }
2537 /* compensate u increment in main loop */
2538 --additional;
2539
2540 /* free the replaced union subtype */
2541 lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux);
2542 } else {
2543 LY_ARRAY_INCREMENT(un->types);
2544 }
2545 LY_CHECK_RET(ret);
2546 }
2547 }
2548
2549 if (!base && !type_p->flags) {
2550 /* type derived from union built-in type must contain at least one type */
2551 if (tpdfname) {
2552 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
2553 } else {
2554 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
2555 free(*type);
2556 *type = NULL;
2557 }
2558 return LY_EVALID;
2559 }
2560
2561 if (tpdfname) {
2562 type_p->compiled = *type;
2563 *type = calloc(1, sizeof(struct lysc_type_union));
2564 }
2565 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002566 case LY_TYPE_BOOL:
2567 case LY_TYPE_EMPTY:
2568 case LY_TYPE_UNKNOWN: /* just to complete switch */
2569 break;
2570 }
2571 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
2572done:
2573 return ret;
2574}
2575
Radek Krejcia3045382018-11-22 14:30:31 +01002576/**
2577 * @brief Compile information about the leaf/leaf-list's type.
2578 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002579 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2580 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2581 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2582 * @param[in] context_name Name of the context node or referencing typedef for logging.
2583 * @param[in] type_p Parsed type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002584 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
2585 * @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 +01002586 * @param[out] units Storage for inheriting units value from the typedefs the current type derives from.
Radek Krejcia3045382018-11-22 14:30:31 +01002587 * @return LY_ERR value.
2588 */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002589static LY_ERR
Radek Krejcicdfecd92018-11-26 11:27:32 +01002590lys_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 +01002591 struct lysp_type *type_p, int options, struct lysc_type **type, const char **units)
Radek Krejci19a96102018-11-15 13:38:09 +01002592{
2593 LY_ERR ret = LY_SUCCESS;
2594 unsigned int u;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002595 int dummyloops = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002596 struct type_context {
2597 const struct lysp_tpdf *tpdf;
2598 struct lysp_node *node;
2599 struct lysp_module *mod;
2600 } *tctx, *tctx_prev = NULL;
2601 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002602 struct lysc_type *base = NULL, *prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01002603 struct ly_set tpdf_chain = {0};
Radek Krejci01342af2019-01-03 15:18:08 +01002604 const char *dflt = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +01002605
2606 (*type) = NULL;
2607
2608 tctx = calloc(1, sizeof *tctx);
2609 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +01002610 for (ret = lysp_type_find(type_p->name, context_node_p, ctx->mod_def->parsed,
Radek Krejci19a96102018-11-15 13:38:09 +01002611 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod);
2612 ret == LY_SUCCESS;
2613 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod,
2614 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) {
2615 if (basetype) {
2616 break;
2617 }
2618
2619 /* check status */
Radek Krejcicdfecd92018-11-26 11:27:32 +01002620 ret = lysc_check_status(ctx, context_flags, context_mod, context_name,
2621 tctx->tpdf->flags, tctx->mod, tctx->node ? tctx->node->name : tctx->tpdf->name);
Radek Krejci19a96102018-11-15 13:38:09 +01002622 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
2623
Radek Krejcicdfecd92018-11-26 11:27:32 +01002624 if (units && !*units) {
2625 /* inherit units */
2626 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units);
2627 }
Radek Krejci01342af2019-01-03 15:18:08 +01002628 if (!dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002629 /* inherit default */
Radek Krejci01342af2019-01-03 15:18:08 +01002630 dflt = tctx->tpdf->dflt;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002631 }
Radek Krejci01342af2019-01-03 15:18:08 +01002632 if (dummyloops && (!units || *units) && dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002633 basetype = ((struct type_context*)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
2634 break;
2635 }
2636
Radek Krejci19a96102018-11-15 13:38:09 +01002637 if (tctx->tpdf->type.compiled) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002638 /* it is not necessary to continue, the rest of the chain was already compiled,
2639 * but we still may need to inherit default and units values, so start dummy loops */
Radek Krejci19a96102018-11-15 13:38:09 +01002640 basetype = tctx->tpdf->type.compiled->basetype;
2641 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
Radek Krejci01342af2019-01-03 15:18:08 +01002642 if ((units && !*units) || !dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002643 dummyloops = 1;
2644 goto preparenext;
2645 } else {
2646 tctx = NULL;
2647 break;
2648 }
Radek Krejci19a96102018-11-15 13:38:09 +01002649 }
2650
2651 /* store information for the following processing */
2652 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
2653
Radek Krejcicdfecd92018-11-26 11:27:32 +01002654preparenext:
Radek Krejci19a96102018-11-15 13:38:09 +01002655 /* prepare next loop */
2656 tctx_prev = tctx;
2657 tctx = calloc(1, sizeof *tctx);
2658 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
2659 }
2660 free(tctx);
2661
2662 /* allocate type according to the basetype */
2663 switch (basetype) {
2664 case LY_TYPE_BINARY:
2665 *type = calloc(1, sizeof(struct lysc_type_bin));
Radek Krejci19a96102018-11-15 13:38:09 +01002666 break;
2667 case LY_TYPE_BITS:
2668 *type = calloc(1, sizeof(struct lysc_type_bits));
Radek Krejci19a96102018-11-15 13:38:09 +01002669 break;
2670 case LY_TYPE_BOOL:
2671 case LY_TYPE_EMPTY:
2672 *type = calloc(1, sizeof(struct lysc_type));
2673 break;
2674 case LY_TYPE_DEC64:
2675 *type = calloc(1, sizeof(struct lysc_type_dec));
2676 break;
2677 case LY_TYPE_ENUM:
2678 *type = calloc(1, sizeof(struct lysc_type_enum));
Radek Krejci19a96102018-11-15 13:38:09 +01002679 break;
2680 case LY_TYPE_IDENT:
2681 *type = calloc(1, sizeof(struct lysc_type_identityref));
2682 break;
2683 case LY_TYPE_INST:
2684 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2685 break;
2686 case LY_TYPE_LEAFREF:
2687 *type = calloc(1, sizeof(struct lysc_type_leafref));
2688 break;
2689 case LY_TYPE_STRING:
2690 *type = calloc(1, sizeof(struct lysc_type_str));
Radek Krejci19a96102018-11-15 13:38:09 +01002691 break;
2692 case LY_TYPE_UNION:
2693 *type = calloc(1, sizeof(struct lysc_type_union));
2694 break;
2695 case LY_TYPE_INT8:
2696 case LY_TYPE_UINT8:
2697 case LY_TYPE_INT16:
2698 case LY_TYPE_UINT16:
2699 case LY_TYPE_INT32:
2700 case LY_TYPE_UINT32:
2701 case LY_TYPE_INT64:
2702 case LY_TYPE_UINT64:
2703 *type = calloc(1, sizeof(struct lysc_type_num));
Radek Krejci19a96102018-11-15 13:38:09 +01002704 break;
2705 case LY_TYPE_UNKNOWN:
2706 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2707 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
2708 ret = LY_EVALID;
2709 goto cleanup;
2710 }
2711 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002712 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci19a96102018-11-15 13:38:09 +01002713 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
2714 ly_data_type2str[basetype]);
2715 free(*type);
2716 (*type) = NULL;
2717 ret = LY_EVALID;
2718 goto cleanup;
2719 }
2720
2721 /* get restrictions from the referred typedefs */
2722 for (u = tpdf_chain.count - 1; u + 1 > 0; --u) {
2723 tctx = (struct type_context*)tpdf_chain.objs[u];
Radek Krejci43699232018-11-23 14:59:46 +01002724 if (tctx->tpdf->type.compiled) {
Radek Krejci19a96102018-11-15 13:38:09 +01002725 base = tctx->tpdf->type.compiled;
2726 continue;
Radek Krejci43699232018-11-23 14:59:46 +01002727 } else if (basetype != LY_TYPE_LEAFREF && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) {
Radek Krejci19a96102018-11-15 13:38:09 +01002728 /* no change, just use the type information from the base */
2729 base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled;
2730 ++base->refcount;
2731 continue;
2732 }
2733
2734 ++(*type)->refcount;
Radek Krejci43699232018-11-23 14:59:46 +01002735 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
2736 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
2737 tctx->tpdf->name, ly_data_type2str[basetype]);
2738 ret = LY_EVALID;
2739 goto cleanup;
2740 } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) {
2741 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2742 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
2743 tctx->tpdf->name, tctx->tpdf->dflt);
2744 ret = LY_EVALID;
2745 goto cleanup;
2746 }
2747
Radek Krejci19a96102018-11-15 13:38:09 +01002748 (*type)->basetype = basetype;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002749 prev_type = *type;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002750 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 +01002751 basetype & (LY_TYPE_LEAFREF | LY_TYPE_UNION) ? lysp_find_module(ctx->ctx, tctx->mod) : NULL,
2752 basetype, options, tctx->tpdf->name, base, type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002753 LY_CHECK_GOTO(ret, cleanup);
2754 base = prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01002755 }
2756
Radek Krejcic5c27e52018-11-15 14:38:11 +01002757 /* process the type definition in leaf */
Radek Krejcicdfecd92018-11-26 11:27:32 +01002758 if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) {
Radek Krejcia3045382018-11-22 14:30:31 +01002759 /* get restrictions from the node itself */
Radek Krejci19a96102018-11-15 13:38:09 +01002760 (*type)->basetype = basetype;
2761 ++(*type)->refcount;
Radek Krejcie86bf772018-12-14 11:39:53 +01002762 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 +01002763 LY_CHECK_GOTO(ret, cleanup);
2764 } else {
Radek Krejci19a96102018-11-15 13:38:09 +01002765 /* no specific restriction in leaf's type definition, copy from the base */
2766 free(*type);
2767 (*type) = base;
2768 ++(*type)->refcount;
Radek Krejci19a96102018-11-15 13:38:09 +01002769 }
Radek Krejci01342af2019-01-03 15:18:08 +01002770 if (!(*type)->dflt) {
2771 DUP_STRING(ctx->ctx, dflt, (*type)->dflt);
2772 }
Radek Krejci19a96102018-11-15 13:38:09 +01002773
2774 COMPILE_ARRAY_GOTO(ctx, type_p->exts, (*type)->exts, options, u, lys_compile_ext, ret, cleanup);
2775
2776cleanup:
2777 ly_set_erase(&tpdf_chain, free);
2778 return ret;
2779}
2780
Radek Krejcib1b59152019-01-07 13:21:56 +01002781static 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 +01002782
Radek Krejcia3045382018-11-22 14:30:31 +01002783/**
2784 * @brief Compile parsed container node information.
2785 * @param[in] ctx Compile context
2786 * @param[in] node_p Parsed container node.
2787 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
2788 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2789 * is enriched with the container-specific information.
2790 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2791 */
Radek Krejci19a96102018-11-15 13:38:09 +01002792static LY_ERR
2793lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
2794{
2795 struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p;
2796 struct lysc_node_container *cont = (struct lysc_node_container*)node;
2797 struct lysp_node *child_p;
2798 unsigned int u;
2799 LY_ERR ret = LY_SUCCESS;
2800
Radek Krejci19a96102018-11-15 13:38:09 +01002801 LY_LIST_FOR(cont_p->child, child_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01002802 LY_CHECK_RET(lys_compile_node(ctx, child_p, options, node, 0));
Radek Krejci19a96102018-11-15 13:38:09 +01002803 }
2804
2805 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, options, u, lys_compile_must, ret, done);
2806 //COMPILE_ARRAY_GOTO(ctx, cont_p->actions, cont->actions, options, u, lys_compile_action, ret, done);
2807 //COMPILE_ARRAY_GOTO(ctx, cont_p->notifs, cont->notifs, options, u, lys_compile_notif, ret, done);
2808
2809done:
2810 return ret;
2811}
2812
Radek Krejcia3045382018-11-22 14:30:31 +01002813/**
2814 * @brief Compile parsed leaf node information.
2815 * @param[in] ctx Compile context
2816 * @param[in] node_p Parsed leaf node.
2817 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
2818 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2819 * is enriched with the leaf-specific information.
2820 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2821 */
Radek Krejci19a96102018-11-15 13:38:09 +01002822static LY_ERR
2823lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
2824{
2825 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p;
2826 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
2827 unsigned int u;
2828 LY_ERR ret = LY_SUCCESS;
2829
Radek Krejci19a96102018-11-15 13:38:09 +01002830 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, options, u, lys_compile_must, ret, done);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002831 DUP_STRING(ctx->ctx, leaf_p->units, leaf->units);
2832 DUP_STRING(ctx->ctx, leaf_p->dflt, leaf->dflt);
Radek Krejci76b3e962018-12-14 17:01:25 +01002833 if (leaf->dflt) {
2834 leaf->flags |= LYS_SET_DFLT;
2835 }
Radek Krejci43699232018-11-23 14:59:46 +01002836
Radek Krejcie86bf772018-12-14 11:39:53 +01002837 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 +01002838 leaf->units ? NULL : &leaf->units);
Radek Krejci19a96102018-11-15 13:38:09 +01002839 LY_CHECK_GOTO(ret, done);
Radek Krejci01342af2019-01-03 15:18:08 +01002840 if (!leaf->dflt && !(leaf->flags & LYS_MAND_TRUE)) {
2841 DUP_STRING(ctx->ctx, leaf->type->dflt, leaf->dflt);
2842 }
Radek Krejcia3045382018-11-22 14:30:31 +01002843 if (leaf->type->basetype == LY_TYPE_LEAFREF) {
2844 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
2845 ly_set_add(&ctx->unres, leaf, 0);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002846 } else if (leaf->type->basetype == LY_TYPE_UNION) {
2847 LY_ARRAY_FOR(((struct lysc_type_union*)leaf->type)->types, u) {
2848 if (((struct lysc_type_union*)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
2849 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
2850 ly_set_add(&ctx->unres, leaf, 0);
2851 }
2852 }
Radek Krejci43699232018-11-23 14:59:46 +01002853 } else if (leaf->type->basetype == LY_TYPE_EMPTY && leaf_p->dflt) {
2854 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2855 "Leaf of type \"empty\" must not have a default value (%s).",leaf_p->dflt);
2856 return LY_EVALID;
Radek Krejcia3045382018-11-22 14:30:31 +01002857 }
2858
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01002859 /* TODO validate default value according to the type, possibly postpone the check when the leafref target is known */
2860
Radek Krejci19a96102018-11-15 13:38:09 +01002861done:
2862 return ret;
2863}
2864
Radek Krejcia3045382018-11-22 14:30:31 +01002865/**
Radek Krejci0e5d8382018-11-28 16:37:53 +01002866 * @brief Compile parsed leaf-list node information.
2867 * @param[in] ctx Compile context
2868 * @param[in] node_p Parsed leaf-list node.
2869 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
2870 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2871 * is enriched with the leaf-list-specific information.
2872 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2873 */
2874static LY_ERR
2875lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
2876{
2877 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p;
2878 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
2879 unsigned int u, v;
Radek Krejci0e5d8382018-11-28 16:37:53 +01002880 LY_ERR ret = LY_SUCCESS;
2881
Radek Krejci0e5d8382018-11-28 16:37:53 +01002882 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, options, u, lys_compile_must, ret, done);
2883 DUP_STRING(ctx->ctx, llist_p->units, llist->units);
2884
2885 if (llist_p->dflts) {
2886 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(llist_p->dflts), ret, done);
2887 LY_ARRAY_FOR(llist_p->dflts, u) {
2888 DUP_STRING(ctx->ctx, llist_p->dflts[u], llist->dflts[u]);
2889 LY_ARRAY_INCREMENT(llist->dflts);
2890 }
2891 }
2892
2893 llist->min = llist_p->min;
Radek Krejcib7408632018-11-28 17:12:11 +01002894 llist->max = llist_p->max ? llist_p->max : (uint32_t)-1;
Radek Krejci0e5d8382018-11-28 16:37:53 +01002895
Radek Krejcie86bf772018-12-14 11:39:53 +01002896 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 +01002897 llist->units ? NULL : &llist->units);
Radek Krejci0e5d8382018-11-28 16:37:53 +01002898 LY_CHECK_GOTO(ret, done);
Radek Krejci01342af2019-01-03 15:18:08 +01002899 if (llist->type->dflt && !llist->dflts && !llist->min) {
Radek Krejci0e5d8382018-11-28 16:37:53 +01002900 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, 1, ret, done);
Radek Krejci01342af2019-01-03 15:18:08 +01002901 DUP_STRING(ctx->ctx, llist->type->dflt, llist->dflts[0]);
Radek Krejci0e5d8382018-11-28 16:37:53 +01002902 LY_ARRAY_INCREMENT(llist->dflts);
2903 }
2904
2905 if (llist->type->basetype == LY_TYPE_LEAFREF) {
2906 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
2907 ly_set_add(&ctx->unres, llist, 0);
2908 } else if (llist->type->basetype == LY_TYPE_UNION) {
2909 LY_ARRAY_FOR(((struct lysc_type_union*)llist->type)->types, u) {
2910 if (((struct lysc_type_union*)llist->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
2911 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
2912 ly_set_add(&ctx->unres, llist, 0);
2913 }
2914 }
Radek Krejci6bb080b2018-11-28 17:23:41 +01002915 } else if (llist->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002916 if (ctx->mod_def->version < LYS_VERSION_1_1) {
Radek Krejci6bb080b2018-11-28 17:23:41 +01002917 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2918 "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
2919 return LY_EVALID;
2920 } else if (llist_p->dflts) {
2921 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2922 "Leaf-list of type \"empty\" must not have a default value (%s).", llist_p->dflts[0]);
2923 return LY_EVALID;
2924 }
Radek Krejci0e5d8382018-11-28 16:37:53 +01002925 }
2926
2927 if ((llist->flags & LYS_CONFIG_W) && llist->dflts && LY_ARRAY_SIZE(llist->dflts)) {
2928 /* configuration data values must be unique - so check the default values */
2929 LY_ARRAY_FOR(llist->dflts, u) {
2930 for (v = u + 1; v < LY_ARRAY_SIZE(llist->dflts); ++v) {
2931 if (!strcmp(llist->dflts[u], llist->dflts[v])) {
2932 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2933 "Configuration leaf-list has multiple defaults of the same value \"%s\".", llist->dflts[v]);
2934 return LY_EVALID;
2935 }
2936 }
2937 }
2938 }
2939
2940 /* TODO validate default value according to the type, possibly postpone the check when the leafref target is known */
2941
2942done:
2943 return ret;
2944}
2945
2946/**
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002947 * @brief Compile parsed list node information.
2948 * @param[in] ctx Compile context
2949 * @param[in] node_p Parsed list node.
2950 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
2951 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2952 * is enriched with the list-specific information.
2953 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2954 */
2955static LY_ERR
2956lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
2957{
2958 struct lysp_node_list *list_p = (struct lysp_node_list*)node_p;
2959 struct lysc_node_list *list = (struct lysc_node_list*)node;
2960 struct lysp_node *child_p;
2961 struct lysc_node_leaf **key, ***unique;
2962 size_t len;
2963 unsigned int u, v;
2964 const char *keystr, *delim;
2965 int config;
2966 LY_ERR ret = LY_SUCCESS;
2967
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002968 list->min = list_p->min;
2969 list->max = list_p->max ? list_p->max : (uint32_t)-1;
2970
2971 LY_LIST_FOR(list_p->child, child_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01002972 LY_CHECK_RET(lys_compile_node(ctx, child_p, options, node, 0));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002973 }
2974
2975 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, options, u, lys_compile_must, ret, done);
2976
2977 /* keys */
2978 if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) {
2979 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
2980 return LY_EVALID;
2981 }
2982
2983 /* find all the keys (must be direct children) */
2984 keystr = list_p->key;
2985 while (keystr) {
2986 delim = strpbrk(keystr, " \t\n");
2987 if (delim) {
2988 len = delim - keystr;
2989 while (isspace(*delim)) {
2990 ++delim;
2991 }
2992 } else {
2993 len = strlen(keystr);
2994 }
2995
2996 /* key node must be present */
2997 LY_ARRAY_NEW_RET(ctx->ctx, list->keys, key, LY_EMEM);
2998 *key = (struct lysc_node_leaf*)lys_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE | LYS_GETNEXT_NOSTATECHECK);
2999 if (!(*key)) {
3000 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3001 "The list's key \"%.*s\" not found.", len, keystr);
3002 return LY_EVALID;
3003 }
3004 /* keys must be unique */
3005 for(u = 0; u < LY_ARRAY_SIZE(list->keys) - 1; ++u) {
3006 if (*key == list->keys[u]) {
3007 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3008 "Duplicated key identifier \"%.*s\".", len, keystr);
3009 return LY_EVALID;
3010 }
3011 }
3012 /* key must have the same config flag as the list itself */
3013 if ((list->flags & LYS_CONFIG_MASK) != ((*key)->flags & LYS_CONFIG_MASK)) {
3014 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf.");
3015 return LY_EVALID;
3016 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003017 if (ctx->mod_def->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003018 /* YANG 1.0 denies key to be of empty type */
3019 if ((*key)->type->basetype == LY_TYPE_EMPTY) {
3020 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3021 "Key of a list can be of type \"empty\" only in YANG 1.1 modules.");
3022 return LY_EVALID;
3023 }
3024 } else {
3025 /* when and if-feature are illegal on list keys */
3026 if ((*key)->when) {
3027 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3028 "List's key \"%s\" must not have any \"when\" statement.", (*key)->name);
3029 return LY_EVALID;
3030 }
3031 if ((*key)->iffeatures) {
3032 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3033 "List's key \"%s\" must not have any \"if-feature\" statement.", (*key)->name);
3034 return LY_EVALID;
3035 }
3036 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003037
3038 /* check status */
3039 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3040 (*key)->flags, (*key)->module, (*key)->name));
3041
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003042 /* ignore default values of the key */
3043 if ((*key)->dflt) {
3044 lydict_remove(ctx->ctx, (*key)->dflt);
3045 (*key)->dflt = NULL;
3046 }
3047 /* mark leaf as key */
3048 (*key)->flags |= LYS_KEY;
3049
3050 /* next key value */
3051 keystr = delim;
3052 }
3053
3054 /* uniques */
3055 if (list_p->uniques) {
3056 for (v = 0; v < LY_ARRAY_SIZE(list_p->uniques); ++v) {
3057 config = -1;
3058 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
3059 keystr = list_p->uniques[v];
3060 while (keystr) {
3061 delim = strpbrk(keystr, " \t\n");
3062 if (delim) {
3063 len = delim - keystr;
3064 while (isspace(*delim)) {
3065 ++delim;
3066 }
3067 } else {
3068 len = strlen(keystr);
3069 }
3070
3071 /* unique node must be present */
3072 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
Radek Krejci95710c92019-02-11 15:49:55 +01003073 ret = lys_resolve_schema_nodeid(ctx, keystr, len, node, LYS_LEAF, 0, (const struct lysc_node**)key);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003074 if (ret != LY_SUCCESS) {
3075 if (ret == LY_EDENIED) {
3076 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3077 "Unique's descendant-schema-nodeid \"%.*s\" refers to a %s node instead of a leaf.",
3078 len, keystr, lys_nodetype2str((*key)->nodetype));
3079 }
3080 return LY_EVALID;
3081 }
3082
3083 /* all referenced leafs must be of the same config type */
3084 if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) {
3085 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3086 "Unique statement \"%s\" refers to leafs with different config type.", list_p->uniques[v]);
3087 return LY_EVALID;
3088 } else if ((*key)->flags & LYS_CONFIG_W) {
3089 config = 1;
3090 } else { /* LYS_CONFIG_R */
3091 config = 0;
3092 }
3093
Radek Krejci76b3e962018-12-14 17:01:25 +01003094 /* check status */
3095 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3096 (*key)->flags, (*key)->module, (*key)->name));
3097
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003098 /* mark leaf as unique */
3099 (*key)->flags |= LYS_UNIQUE;
3100
3101 /* next unique value in line */
3102 keystr = delim;
3103 }
3104 /* next unique definition */
3105 }
3106 }
3107
3108 //COMPILE_ARRAY_GOTO(ctx, list_p->actions, list->actions, options, u, lys_compile_action, ret, done);
3109 //COMPILE_ARRAY_GOTO(ctx, list_p->notifs, list->notifs, options, u, lys_compile_notif, ret, done);
3110
3111done:
3112 return ret;
3113}
3114
Radek Krejci76b3e962018-12-14 17:01:25 +01003115static LY_ERR
3116lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
3117{
3118 struct lysc_node *iter, *node = (struct lysc_node*)ch;
3119 const char *prefix = NULL, *name;
3120 size_t prefix_len = 0;
3121
3122 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3123 name = strchr(dflt, ':');
3124 if (name) {
3125 prefix = dflt;
3126 prefix_len = name - prefix;
3127 ++name;
3128 } else {
3129 name = dflt;
3130 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003131 if (prefix && (strncmp(prefix, node->module->prefix, prefix_len) || node->module->prefix[prefix_len] != '\0')) {
Radek Krejci76b3e962018-12-14 17:01:25 +01003132 /* prefixed default case make sense only for the prefix of the schema itself */
3133 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3134 "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").",
3135 prefix_len, prefix);
3136 return LY_EVALID;
3137 }
3138 ch->dflt = (struct lysc_node_case*)lys_child(node, node->module, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE);
3139 if (!ch->dflt) {
3140 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3141 "Default case \"%s\" not found.", dflt);
3142 return LY_EVALID;
3143 }
3144 /* no mandatory nodes directly under the default case */
3145 LY_LIST_FOR(ch->dflt->child, iter) {
3146 if (iter->flags & LYS_MAND_TRUE) {
3147 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3148 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt);
3149 return LY_EVALID;
3150 }
3151 }
Radek Krejci01342af2019-01-03 15:18:08 +01003152 ch->dflt->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01003153 return LY_SUCCESS;
3154}
3155
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003156/**
Radek Krejci056d0a82018-12-06 16:57:25 +01003157 * @brief Compile parsed choice node information.
3158 * @param[in] ctx Compile context
3159 * @param[in] node_p Parsed choice node.
3160 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3161 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
Radek Krejci76b3e962018-12-14 17:01:25 +01003162 * is enriched with the choice-specific information.
Radek Krejci056d0a82018-12-06 16:57:25 +01003163 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3164 */
3165static LY_ERR
3166lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
3167{
3168 struct lysp_node_choice *ch_p = (struct lysp_node_choice*)node_p;
3169 struct lysc_node_choice *ch = (struct lysc_node_choice*)node;
3170 struct lysp_node *child_p, *case_child_p;
Radek Krejcia9026eb2018-12-12 16:04:47 +01003171 struct lys_module;
Radek Krejci056d0a82018-12-06 16:57:25 +01003172 LY_ERR ret = LY_SUCCESS;
3173
Radek Krejci056d0a82018-12-06 16:57:25 +01003174 LY_LIST_FOR(ch_p->child, child_p) {
3175 if (child_p->nodetype == LYS_CASE) {
3176 LY_LIST_FOR(((struct lysp_node_case*)child_p)->child, case_child_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01003177 LY_CHECK_RET(lys_compile_node(ctx, case_child_p, options, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01003178 }
3179 } else {
Radek Krejcib1b59152019-01-07 13:21:56 +01003180 LY_CHECK_RET(lys_compile_node(ctx, child_p, options, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01003181 }
3182 }
3183
3184 /* default branch */
Radek Krejcia9026eb2018-12-12 16:04:47 +01003185 if (ch_p->dflt) {
Radek Krejci76b3e962018-12-14 17:01:25 +01003186 LY_CHECK_RET(lys_compile_node_choice_dflt(ctx, ch_p->dflt, ch));
Radek Krejcia9026eb2018-12-12 16:04:47 +01003187 }
Radek Krejci056d0a82018-12-06 16:57:25 +01003188
Radek Krejci9800fb82018-12-13 14:26:23 +01003189 return ret;
3190}
3191
3192/**
3193 * @brief Compile parsed anydata or anyxml node information.
3194 * @param[in] ctx Compile context
3195 * @param[in] node_p Parsed anydata or anyxml node.
3196 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3197 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3198 * is enriched with the any-specific information.
3199 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3200 */
3201static LY_ERR
3202lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
3203{
3204 struct lysp_node_anydata *any_p = (struct lysp_node_anydata*)node_p;
3205 struct lysc_node_anydata *any = (struct lysc_node_anydata*)node;
3206 unsigned int u;
3207 LY_ERR ret = LY_SUCCESS;
3208
3209 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, options, u, lys_compile_must, ret, done);
3210
3211 if (any->flags & LYS_CONFIG_W) {
3212 LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended.",
3213 ly_stmt2str(any->nodetype == LYS_ANYDATA ? YANG_ANYDATA : YANG_ANYXML));
3214 }
Radek Krejci056d0a82018-12-06 16:57:25 +01003215done:
3216 return ret;
3217}
3218
3219static LY_ERR
Radek Krejci76b3e962018-12-14 17:01:25 +01003220lys_compile_status_check(struct lysc_ctx *ctx, uint16_t node_flags, uint16_t parent_flags)
3221{
3222 /* check status compatibility with the parent */
3223 if ((parent_flags & LYS_STATUS_MASK) > (node_flags & LYS_STATUS_MASK)) {
3224 if (node_flags & LYS_STATUS_CURR) {
3225 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3226 "A \"current\" status is in conflict with the parent's \"%s\" status.",
3227 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3228 } else { /* LYS_STATUS_DEPRC */
3229 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3230 "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
3231 }
3232 return LY_EVALID;
3233 }
3234 return LY_SUCCESS;
3235}
3236
3237static LY_ERR
Radek Krejcib1b59152019-01-07 13:21:56 +01003238lys_compile_status(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t parent_flags)
Radek Krejci056d0a82018-12-06 16:57:25 +01003239{
Radek Krejci056d0a82018-12-06 16:57:25 +01003240 /* status - it is not inherited by specification, but it does not make sense to have
3241 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3242 if (!(node->flags & LYS_STATUS_MASK)) {
Radek Krejcib1b59152019-01-07 13:21:56 +01003243 if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) {
3244 if ((parent_flags & 0x3) != 0x3) {
3245 /* do not print the warning when inheriting status from uses - the uses_status value has a special
3246 * combination of bits (0x3) which marks the uses_status value */
3247 LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.",
3248 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3249 }
3250 node->flags |= parent_flags & LYS_STATUS_MASK;
Radek Krejci056d0a82018-12-06 16:57:25 +01003251 } else {
3252 node->flags |= LYS_STATUS_CURR;
3253 }
Radek Krejcib1b59152019-01-07 13:21:56 +01003254 } else if (parent_flags & LYS_STATUS_MASK) {
3255 return lys_compile_status_check(ctx, node->flags, parent_flags);
Radek Krejci056d0a82018-12-06 16:57:25 +01003256 }
3257 return LY_SUCCESS;
3258}
3259
3260static LY_ERR
3261lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *children,
3262 const struct lysc_action *actions, const struct lysc_notif *notifs,
3263 const char *name, void *exclude)
3264{
3265 const struct lysc_node *iter;
3266 unsigned int u;
3267
3268 LY_LIST_FOR(children, iter) {
Radek Krejci95710c92019-02-11 15:49:55 +01003269 if (iter != exclude && iter->module == ctx->mod && !strcmp(name, iter->name)) {
Radek Krejci056d0a82018-12-06 16:57:25 +01003270 goto error;
3271 }
3272 }
3273 LY_ARRAY_FOR(actions, u) {
Radek Krejci95710c92019-02-11 15:49:55 +01003274 if (&actions[u] != exclude && actions[u].module == ctx->mod && !strcmp(name, actions[u].name)) {
Radek Krejci056d0a82018-12-06 16:57:25 +01003275 goto error;
3276 }
3277 }
3278 LY_ARRAY_FOR(notifs, u) {
Radek Krejci95710c92019-02-11 15:49:55 +01003279 if (&notifs[u] != exclude && notifs[u].module == ctx->mod && !strcmp(name, notifs[u].name)) {
Radek Krejci056d0a82018-12-06 16:57:25 +01003280 goto error;
3281 }
3282 }
3283 return LY_SUCCESS;
3284error:
3285 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "data definition");
3286 return LY_EEXIST;
3287}
3288
3289/**
3290 * @brief Connect the node into the siblings list and check its name uniqueness.
3291 *
3292 * @param[in] ctx Compile context
3293 * @param[in] parent Parent node holding the children list, in case of node from a choice's case,
3294 * the choice itself is expected instead of a specific case node.
3295 * @param[in] node Schema node to connect into the list.
3296 * @return LY_ERR value - LY_SUCCESS or LY_EEXIST.
3297 */
3298static LY_ERR
3299lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node)
3300{
3301 struct lysc_node **children;
3302
3303 if (node->nodetype == LYS_CASE) {
3304 children = (struct lysc_node**)&((struct lysc_node_choice*)parent)->cases;
3305 } else {
3306 children = lysc_node_children_p(parent);
3307 }
3308 if (children) {
3309 if (!(*children)) {
3310 /* first child */
3311 *children = node;
3312 } else if (*children != node) {
3313 /* by the condition in previous branch we cover the choice/case children
3314 * - the children list is shared by the choice and the the first case, in addition
3315 * the first child of each case must be referenced from the case node. So the node is
3316 * actually always already inserted in case it is the first children - so here such
3317 * a situation actually corresponds to the first branch */
3318 /* insert at the end of the parent's children list */
3319 (*children)->prev->next = node;
3320 node->prev = (*children)->prev;
3321 (*children)->prev = node;
3322
3323 /* check the name uniqueness */
3324 if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent),
3325 lysc_node_notifs(parent), node->name, node)) {
3326 return LY_EEXIST;
3327 }
3328 }
3329 }
3330 return LY_SUCCESS;
3331}
3332
Radek Krejci95710c92019-02-11 15:49:55 +01003333/**
3334 * @param[in] node_p Node image from the parsed tree. If the case is explicit, it is the LYS_CASE node, but in case of implicit case,
3335 * it is the LYS_CHOICE node or LYS_AUGMENT node.
3336 */
Radek Krejci056d0a82018-12-06 16:57:25 +01003337static struct lysc_node_case*
3338lys_compile_node_case(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node_choice *ch, struct lysc_node *child)
3339{
3340 struct lysc_node *iter;
3341 struct lysc_node_case *cs;
Radek Krejci00b874b2019-02-12 10:54:50 +01003342 struct lysc_when **when;
Radek Krejci056d0a82018-12-06 16:57:25 +01003343 unsigned int u;
3344 LY_ERR ret;
3345
Radek Krejci95710c92019-02-11 15:49:55 +01003346#define UNIQUE_CHECK(NAME, MOD) \
Radek Krejci056d0a82018-12-06 16:57:25 +01003347 LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \
Radek Krejci95710c92019-02-11 15:49:55 +01003348 if (iter->module == MOD && !strcmp(iter->name, NAME)) { \
Radek Krejci056d0a82018-12-06 16:57:25 +01003349 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \
3350 return NULL; \
3351 } \
3352 }
3353
Radek Krejci95710c92019-02-11 15:49:55 +01003354 if (node_p->nodetype == LYS_CHOICE || node_p->nodetype == LYS_AUGMENT) {
3355 UNIQUE_CHECK(child->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01003356
3357 /* we have to add an implicit case node into the parent choice */
3358 cs = calloc(1, sizeof(struct lysc_node_case));
3359 DUP_STRING(ctx->ctx, child->name, cs->name);
3360 cs->flags = ch->flags & LYS_STATUS_MASK;
Radek Krejci95710c92019-02-11 15:49:55 +01003361 } else if (node_p->nodetype == LYS_CASE) {
Radek Krejci056d0a82018-12-06 16:57:25 +01003362 if (ch->cases && (node_p == ch->cases->prev->sp)) {
3363 /* the case is already present since the child is not its first children */
3364 return (struct lysc_node_case*)ch->cases->prev;
3365 }
Radek Krejci95710c92019-02-11 15:49:55 +01003366 UNIQUE_CHECK(node_p->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01003367
3368 /* explicit parent case is not present (this is its first child) */
3369 cs = calloc(1, sizeof(struct lysc_node_case));
3370 DUP_STRING(ctx->ctx, node_p->name, cs->name);
3371 cs->flags = LYS_STATUS_MASK & node_p->flags;
3372 cs->sp = node_p;
3373
Radek Krejcib1b59152019-01-07 13:21:56 +01003374 /* check the case's status (don't need to solve uses_status since case statement cannot be directly in grouping statement */
3375 LY_CHECK_RET(lys_compile_status(ctx, (struct lysc_node*)cs, ch->flags), NULL);
Radek Krejci00b874b2019-02-12 10:54:50 +01003376
3377 if (node_p->when) {
3378 LY_ARRAY_NEW_GOTO(ctx->ctx, cs->when, when, ret, error);
3379 ret = lys_compile_when(ctx, node_p->when, options, when);
3380 LY_CHECK_GOTO(ret, error);
3381 (*when)->context = lys_compile_xpath_context(ch->parent);
3382 }
Radek Krejci056d0a82018-12-06 16:57:25 +01003383 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, cs->iffeatures, options, u, lys_compile_iffeature, ret, error);
Radek Krejci95710c92019-02-11 15:49:55 +01003384 } else {
3385 LOGINT(ctx->ctx);
3386 goto error;
Radek Krejci056d0a82018-12-06 16:57:25 +01003387 }
3388 cs->module = ctx->mod;
3389 cs->prev = (struct lysc_node*)cs;
3390 cs->nodetype = LYS_CASE;
3391 lys_compile_node_connect(ctx, (struct lysc_node*)ch, (struct lysc_node*)cs);
3392 cs->parent = (struct lysc_node*)ch;
3393 cs->child = child;
3394
3395 return cs;
3396error:
3397 return NULL;
3398
3399#undef UNIQUE_CHECK
3400}
3401
Radek Krejci76b3e962018-12-14 17:01:25 +01003402static LY_ERR
3403lys_compile_refine_config(struct lysc_ctx *ctx, struct lysc_node *node, struct lysp_refine *rfn, int inheriting)
3404{
3405 struct lysc_node *child;
3406 uint16_t config = rfn->flags & LYS_CONFIG_MASK;
3407
3408 if (config == (node->flags & LYS_CONFIG_MASK)) {
3409 /* nothing to do */
3410 return LY_SUCCESS;
3411 }
3412
3413 if (!inheriting) {
3414 /* explicit refine */
3415 if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) {
3416 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejcib1b59152019-01-07 13:21:56 +01003417 "Invalid refine of config in \"%s\" - configuration node cannot be child of any state data node.",
3418 rfn->nodeid);
Radek Krejci76b3e962018-12-14 17:01:25 +01003419 return LY_EVALID;
3420 }
3421 }
3422 node->flags &= ~LYS_CONFIG_MASK;
3423 node->flags |= config;
3424
3425 /* inherit the change into the children */
3426 LY_LIST_FOR((struct lysc_node*)lysc_node_children(node), child) {
3427 LY_CHECK_RET(lys_compile_refine_config(ctx, child, rfn, 1));
3428 }
3429
3430 /* TODO actions and notifications */
3431
3432 return LY_SUCCESS;
3433}
3434
Radek Krejci056d0a82018-12-06 16:57:25 +01003435/**
Radek Krejcie86bf772018-12-14 11:39:53 +01003436 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
3437 * If present, also apply uses's modificators.
3438 *
3439 * @param[in] ctx Compile context
3440 * @param[in] uses_p Parsed uses schema node.
3441 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3442 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
3443 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
3444 * the compile context.
3445 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3446 */
3447static LY_ERR
3448lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, int options, struct lysc_node *parent)
3449{
3450 struct lysp_node *node_p;
Radek Krejci01342af2019-01-03 15:18:08 +01003451 struct lysc_node *node, *child;
Radek Krejci12fb9142019-01-08 09:45:30 +01003452 /* context_node_fake allows us to temporarily isolate the nodes inserted from the grouping instead of uses */
Radek Krejci01342af2019-01-03 15:18:08 +01003453 struct lysc_node_container context_node_fake =
3454 {.nodetype = LYS_CONTAINER,
3455 .module = ctx->mod,
3456 .flags = parent ? parent->flags : 0,
3457 .child = NULL, .next = NULL,
3458 .prev = (struct lysc_node*)&context_node_fake};
Radek Krejcie86bf772018-12-14 11:39:53 +01003459 const struct lysp_grp *grp = NULL;
Radek Krejci76b3e962018-12-14 17:01:25 +01003460 unsigned int u, v, grp_stack_count;
Radek Krejcie86bf772018-12-14 11:39:53 +01003461 int found;
3462 const char *id, *name, *prefix;
3463 size_t prefix_len, name_len;
3464 struct lys_module *mod, *mod_old;
Radek Krejci76b3e962018-12-14 17:01:25 +01003465 struct lysp_refine *rfn;
Radek Krejcie86bf772018-12-14 11:39:53 +01003466 LY_ERR ret = LY_EVALID;
Radek Krejcif2271f12019-01-07 16:42:23 +01003467 uint32_t min, max;
3468 struct ly_set refined = {0};
Radek Krejci00b874b2019-02-12 10:54:50 +01003469 struct lysc_when **when, *when_shared;
Radek Krejcie86bf772018-12-14 11:39:53 +01003470
3471 /* search for the grouping definition */
3472 found = 0;
3473 id = uses_p->name;
3474 lys_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len);
3475 if (prefix) {
3476 mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len);
3477 if (!mod) {
3478 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3479 "Invalid prefix used for grouping reference (%s).", uses_p->name);
3480 return LY_EVALID;
3481 }
3482 } else {
3483 mod = ctx->mod_def;
3484 }
3485 if (mod == ctx->mod_def) {
3486 for (node_p = uses_p->parent; !found && node_p; node_p = node_p->parent) {
3487 grp = lysp_node_groupings(node_p);
3488 LY_ARRAY_FOR(grp, u) {
3489 if (!strcmp(grp[u].name, name)) {
3490 grp = &grp[u];
3491 found = 1;
3492 break;
3493 }
3494 }
3495 }
3496 }
3497 if (!found) {
Radek Krejci76b3e962018-12-14 17:01:25 +01003498 /* search in top-level groupings of the main module ... */
Radek Krejcie86bf772018-12-14 11:39:53 +01003499 grp = mod->parsed->groupings;
Radek Krejci76b3e962018-12-14 17:01:25 +01003500 if (grp) {
3501 for (u = 0; !found && u < LY_ARRAY_SIZE(grp); ++u) {
3502 if (!strcmp(grp[u].name, name)) {
3503 grp = &grp[u];
3504 found = 1;
3505 }
3506 }
3507 }
3508 if (!found && mod->parsed->includes) {
3509 /* ... and all the submodules */
3510 for (u = 0; !found && u < LY_ARRAY_SIZE(mod->parsed->includes); ++u) {
3511 grp = mod->parsed->includes[u].submodule->groupings;
3512 if (grp) {
3513 for (v = 0; !found && v < LY_ARRAY_SIZE(grp); ++v) {
3514 if (!strcmp(grp[v].name, name)) {
3515 grp = &grp[v];
3516 found = 1;
3517 }
3518 }
3519 }
Radek Krejcie86bf772018-12-14 11:39:53 +01003520 }
3521 }
3522 }
3523 if (!found) {
3524 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3525 "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
3526 return LY_EVALID;
3527 }
3528
3529 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
3530 grp_stack_count = ctx->groupings.count;
3531 ly_set_add(&ctx->groupings, (void*)grp, 0);
3532 if (grp_stack_count == ctx->groupings.count) {
3533 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
3534 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3535 "Grouping \"%s\" references itself through a uses statement.", grp->name);
3536 return LY_EVALID;
3537 }
3538
3539 /* switch context's mod_def */
3540 mod_old = ctx->mod_def;
3541 ctx->mod_def = mod;
3542
3543 /* check status */
3544 LY_CHECK_GOTO(lysc_check_status(ctx, uses_p->flags, mod_old, uses_p->name, grp->flags, mod, grp->name), error);
3545
Radek Krejcie86bf772018-12-14 11:39:53 +01003546 LY_LIST_FOR(grp->data, node_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01003547 /* 0x3 in uses_status is a special bits combination to be able to detect status flags from uses */
3548 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 +01003549 child = parent ? lysc_node_children(parent)->prev : ctx->mod->compiled->data->prev;
Radek Krejci00b874b2019-02-12 10:54:50 +01003550
3551 /* some preparation for applying refines */
3552 if (grp->data == node_p) {
3553 /* remember the first child */
3554 context_node_fake.child = child;
Radek Krejci01342af2019-01-03 15:18:08 +01003555 }
3556 }
Radek Krejci00b874b2019-02-12 10:54:50 +01003557 when_shared = NULL;
Radek Krejci01342af2019-01-03 15:18:08 +01003558 LY_LIST_FOR(context_node_fake.child, child) {
3559 child->parent = (struct lysc_node*)&context_node_fake;
Radek Krejci00b874b2019-02-12 10:54:50 +01003560
3561 /* pass uses's when to all the children */
3562 if (uses_p->when) {
3563 LY_ARRAY_NEW_GOTO(ctx->ctx, child->when, when, ret, error);
3564 if (!when_shared) {
3565 ret = lys_compile_when(ctx, uses_p->when, options, when);
3566 LY_CHECK_GOTO(ret, error);
3567 (*when)->context = lys_compile_xpath_context(parent);
3568 when_shared = *when;
3569 } else {
3570 ++when_shared->refcount;
3571 (*when) = when_shared;
3572 }
3573 }
Radek Krejci01342af2019-01-03 15:18:08 +01003574 }
3575 if (context_node_fake.child) {
3576 child = context_node_fake.child->prev;
3577 context_node_fake.child->prev = parent ? lysc_node_children(parent)->prev : ctx->mod->compiled->data->prev;
Radek Krejci76b3e962018-12-14 17:01:25 +01003578 }
3579
Radek Krejci12fb9142019-01-08 09:45:30 +01003580 /* TODO: apply augment */
3581
Radek Krejcif0089082019-01-07 16:42:01 +01003582 /* reload previous context's mod_def */
3583 ctx->mod_def = mod_old;
3584
Radek Krejci76b3e962018-12-14 17:01:25 +01003585 /* apply refine */
3586 LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) {
Radek Krejci95710c92019-02-11 15:49:55 +01003587 LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, rfn->nodeid, 0, (struct lysc_node*)&context_node_fake, 0, 0, (const struct lysc_node**)&node),
Radek Krejci01342af2019-01-03 15:18:08 +01003588 error);
Radek Krejcif2271f12019-01-07 16:42:23 +01003589 ly_set_add(&refined, node, LY_SET_OPT_USEASLIST);
Radek Krejci76b3e962018-12-14 17:01:25 +01003590
3591 /* default value */
3592 if (rfn->dflts) {
Radek Krejci01342af2019-01-03 15:18:08 +01003593 if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_SIZE(rfn->dflts) > 1) {
Radek Krejci76b3e962018-12-14 17:01:25 +01003594 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3595 "Invalid refine of default in \"%s\" - %s cannot hold %d default values.",
3596 rfn->nodeid, lys_nodetype2str(node->nodetype), LY_ARRAY_SIZE(rfn->dflts));
3597 goto error;
3598 }
3599 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) {
3600 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3601 "Invalid refine of default in \"%s\" - %s cannot hold default value(s).",
3602 rfn->nodeid, lys_nodetype2str(node->nodetype));
3603 goto error;
3604 }
3605 if (node->nodetype == LYS_LEAF) {
3606 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)node)->dflt);
3607 DUP_STRING(ctx->ctx, rfn->dflts[0], ((struct lysc_node_leaf*)node)->dflt);
Radek Krejci01342af2019-01-03 15:18:08 +01003608 node->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01003609 /* TODO check the default value according to type */
3610 } else if (node->nodetype == LYS_LEAFLIST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003611 if (ctx->mod->version < 2) {
Radek Krejci01342af2019-01-03 15:18:08 +01003612 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3613 "Invalid refine of default in leaf-list - the default statement is allowed only in YANG 1.1 modules.");
3614 goto error;
3615 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003616 LY_ARRAY_FOR(((struct lysc_node_leaflist*)node)->dflts, u) {
3617 lydict_remove(ctx->ctx, ((struct lysc_node_leaflist*)node)->dflts[u]);
3618 }
3619 LY_ARRAY_FREE(((struct lysc_node_leaflist*)node)->dflts);
Radek Krejci01342af2019-01-03 15:18:08 +01003620 ((struct lysc_node_leaflist*)node)->dflts = NULL;
Radek Krejci76b3e962018-12-14 17:01:25 +01003621 LY_ARRAY_CREATE_GOTO(ctx->ctx, ((struct lysc_node_leaflist*)node)->dflts, LY_ARRAY_SIZE(rfn->dflts), ret, error);
3622 LY_ARRAY_FOR(rfn->dflts, u) {
3623 LY_ARRAY_INCREMENT(((struct lysc_node_leaflist*)node)->dflts);
3624 DUP_STRING(ctx->ctx, rfn->dflts[u], ((struct lysc_node_leaflist*)node)->dflts[u]);
3625 }
3626 /* TODO check the default values according to type */
3627 } else if (node->nodetype == LYS_CHOICE) {
Radek Krejci01342af2019-01-03 15:18:08 +01003628 if (((struct lysc_node_choice*)node)->dflt) {
3629 /* unset LYS_SET_DFLT from the current default case */
3630 ((struct lysc_node_choice*)node)->dflt->flags &= ~LYS_SET_DFLT;
3631 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003632 LY_CHECK_GOTO(lys_compile_node_choice_dflt(ctx, rfn->dflts[0], (struct lysc_node_choice*)node), error);
3633 }
3634 }
3635
Radek Krejci12fb9142019-01-08 09:45:30 +01003636 /* description */
3637 if (rfn->dsc) {
3638 FREE_STRING(ctx->ctx, node->dsc);
3639 node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0);
3640 }
3641
3642 /* reference */
3643 if (rfn->ref) {
3644 FREE_STRING(ctx->ctx, node->ref);
3645 node->ref = lydict_insert(ctx->ctx, rfn->ref, 0);
3646 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003647
3648 /* config */
3649 if (rfn->flags & LYS_CONFIG_MASK) {
3650 LY_CHECK_GOTO(lys_compile_refine_config(ctx, node, rfn, 0), error);
3651 }
3652
3653 /* mandatory */
3654 if (rfn->flags & LYS_MAND_MASK) {
3655 if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) {
3656 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3657 "Invalid refine of mandatory in \"%s\" - %s cannot hold mandatory statement.",
3658 rfn->nodeid, lys_nodetype2str(node->nodetype));
3659 goto error;
3660 }
3661 /* in compiled flags, only the LYS_MAND_TRUE is present */
3662 if (rfn->flags & LYS_MAND_TRUE) {
3663 /* check if node has default value */
3664 if (node->nodetype & LYS_LEAF) {
3665 if (node->flags & LYS_SET_DFLT) {
3666 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci01342af2019-01-03 15:18:08 +01003667 "Invalid refine of mandatory in \"%s\" - leaf already has \"default\" statement.", rfn->nodeid);
Radek Krejci76b3e962018-12-14 17:01:25 +01003668 goto error;
3669 } else {
3670 /* remove the default value taken from the leaf's type */
3671 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)node)->dflt);
3672 ((struct lysc_node_leaf*)node)->dflt = NULL;
3673 }
3674 } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) {
3675 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci01342af2019-01-03 15:18:08 +01003676 "Invalid refine of mandatory in \"%s\" - choice already has \"default\" statement.", rfn->nodeid);
Radek Krejci76b3e962018-12-14 17:01:25 +01003677 goto error;
3678 }
3679 if (node->parent && (node->parent->flags & LYS_SET_DFLT)) {
3680 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3681 "Invalid refine of mandatory in \"%s\" - %s under the default case.",
3682 rfn->nodeid, lys_nodetype2str(node->nodetype));
3683 goto error;
3684 }
3685
3686 node->flags |= LYS_MAND_TRUE;
3687 } else {
Radek Krejci01342af2019-01-03 15:18:08 +01003688 /* make mandatory false */
Radek Krejci76b3e962018-12-14 17:01:25 +01003689 node->flags &= ~LYS_MAND_TRUE;
Radek Krejci01342af2019-01-03 15:18:08 +01003690 if ((node->nodetype & LYS_LEAF) && !((struct lysc_node_leaf*)node)->dflt) {
3691 /* get the type's default value if any */
3692 DUP_STRING(ctx->ctx, ((struct lysc_node_leaf*)node)->type->dflt, ((struct lysc_node_leaf*)node)->dflt);
3693 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003694 }
3695 }
Radek Krejci9a54f1f2019-01-07 13:47:55 +01003696
3697 /* presence */
3698 if (rfn->presence) {
3699 if (node->nodetype != LYS_CONTAINER) {
3700 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3701 "Invalid refine of presence statement in \"%s\" - %s cannot hold the presence statement.",
3702 rfn->nodeid, lys_nodetype2str(node->nodetype));
3703 goto error;
3704 }
3705 node->flags |= LYS_PRESENCE;
3706 }
Radek Krejci9a564c92019-01-07 14:53:57 +01003707
3708 /* must */
3709 if (rfn->musts) {
3710 switch (node->nodetype) {
3711 case LYS_LEAF:
3712 COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_leaf*)node)->musts, options, u, lys_compile_must, ret, error);
3713 break;
3714 case LYS_LEAFLIST:
3715 COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_leaflist*)node)->musts, options, u, lys_compile_must, ret, error);
3716 break;
3717 case LYS_LIST:
3718 COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_list*)node)->musts, options, u, lys_compile_must, ret, error);
3719 break;
3720 case LYS_CONTAINER:
3721 COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_container*)node)->musts, options, u, lys_compile_must, ret, error);
3722 break;
3723 case LYS_ANYXML:
3724 case LYS_ANYDATA:
3725 COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_anydata*)node)->musts, options, u, lys_compile_must, ret, error);
3726 break;
3727 default:
3728 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3729 "Invalid refine of must statement in \"%s\" - %s cannot hold any must statement.",
3730 rfn->nodeid, lys_nodetype2str(node->nodetype));
3731 goto error;
3732 }
3733 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01003734
3735 /* min/max-elements */
3736 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
3737 switch (node->nodetype) {
3738 case LYS_LEAFLIST:
3739 if (rfn->flags & LYS_SET_MAX) {
3740 ((struct lysc_node_leaflist*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
3741 }
3742 if (rfn->flags & LYS_SET_MIN) {
3743 ((struct lysc_node_leaflist*)node)->min = rfn->min;
3744 }
3745 break;
3746 case LYS_LIST:
3747 if (rfn->flags & LYS_SET_MAX) {
3748 ((struct lysc_node_list*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
3749 }
3750 if (rfn->flags & LYS_SET_MIN) {
3751 ((struct lysc_node_list*)node)->min = rfn->min;
3752 }
3753 break;
3754 default:
3755 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3756 "Invalid refine of %s statement in \"%s\" - %s cannot hold this statement.",
3757 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", rfn->nodeid, lys_nodetype2str(node->nodetype));
3758 goto error;
3759 }
3760 }
Radek Krejcif0089082019-01-07 16:42:01 +01003761
3762 /* if-feature */
3763 if (rfn->iffeatures) {
3764 /* any node in compiled tree can get additional if-feature, so do not check nodetype */
3765 COMPILE_ARRAY_GOTO(ctx, rfn->iffeatures, node->iffeatures, options, u, lys_compile_iffeature, ret, error);
3766 }
Radek Krejci01342af2019-01-03 15:18:08 +01003767 }
3768 /* fix connection of the children nodes from fake context node back into the parent */
3769 if (context_node_fake.child) {
3770 context_node_fake.child->prev = child;
3771 }
3772 LY_LIST_FOR(context_node_fake.child, child) {
3773 child->parent = parent;
Radek Krejcie86bf772018-12-14 11:39:53 +01003774 }
3775
Radek Krejcif2271f12019-01-07 16:42:23 +01003776 /* do some additional checks of the changed nodes when all the refines are applied */
3777 for (u = 0; u < refined.count; ++u) {
3778 node = (struct lysc_node*)refined.objs[u];
3779 rfn = &uses_p->refines[u];
3780
3781 /* check possible conflict with default value (default added, mandatory left true) */
3782 if ((node->flags & LYS_MAND_TRUE) &&
3783 (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) ||
3784 ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) {
3785 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3786 "Invalid refine of default in \"%s\" - the node is mandatory.", rfn->nodeid);
3787 goto error;
3788 }
3789
3790 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
3791 if (node->nodetype == LYS_LIST) {
3792 min = ((struct lysc_node_list*)node)->min;
3793 max = ((struct lysc_node_list*)node)->max;
3794 } else {
3795 min = ((struct lysc_node_leaflist*)node)->min;
3796 max = ((struct lysc_node_leaflist*)node)->max;
3797 }
3798 if (min > max) {
3799 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3800 "Invalid refine of %s statement in \"%s\" - \"min-elements\" is bigger than \"max-elements\".",
3801 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", rfn->nodeid);
3802 goto error;
3803 }
3804 }
3805 }
3806
Radek Krejcie86bf772018-12-14 11:39:53 +01003807 ret = LY_SUCCESS;
3808error:
3809 /* reload previous context's mod_def */
3810 ctx->mod_def = mod_old;
3811 /* remove the grouping from the stack for circular groupings dependency check */
3812 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
3813 assert(ctx->groupings.count == grp_stack_count);
Radek Krejcif2271f12019-01-07 16:42:23 +01003814 ly_set_erase(&refined, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01003815
3816 return ret;
3817}
3818
3819/**
Radek Krejcia3045382018-11-22 14:30:31 +01003820 * @brief Compile parsed schema node information.
3821 * @param[in] ctx Compile context
3822 * @param[in] node_p Parsed schema node.
3823 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3824 * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is
3825 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
3826 * the compile context.
Radek Krejcib1b59152019-01-07 13:21:56 +01003827 * @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).
3828 * Zero means no uses, non-zero value with no status bit set mean the default status.
Radek Krejcia3045382018-11-22 14:30:31 +01003829 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3830 */
Radek Krejci19a96102018-11-15 13:38:09 +01003831static LY_ERR
Radek Krejcib1b59152019-01-07 13:21:56 +01003832lys_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 +01003833{
3834 LY_ERR ret = LY_EVALID;
Radek Krejci056d0a82018-12-06 16:57:25 +01003835 struct lysc_node *node;
3836 struct lysc_node_case *cs;
Radek Krejci00b874b2019-02-12 10:54:50 +01003837 struct lysc_when **when;
Radek Krejci19a96102018-11-15 13:38:09 +01003838 unsigned int u;
3839 LY_ERR (*node_compile_spec)(struct lysc_ctx*, struct lysp_node*, int, struct lysc_node*);
3840
3841 switch (node_p->nodetype) {
3842 case LYS_CONTAINER:
3843 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container));
3844 node_compile_spec = lys_compile_node_container;
3845 break;
3846 case LYS_LEAF:
3847 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf));
3848 node_compile_spec = lys_compile_node_leaf;
3849 break;
3850 case LYS_LIST:
3851 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003852 node_compile_spec = lys_compile_node_list;
Radek Krejci19a96102018-11-15 13:38:09 +01003853 break;
3854 case LYS_LEAFLIST:
3855 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist));
Radek Krejci0e5d8382018-11-28 16:37:53 +01003856 node_compile_spec = lys_compile_node_leaflist;
Radek Krejci19a96102018-11-15 13:38:09 +01003857 break;
Radek Krejci19a96102018-11-15 13:38:09 +01003858 case LYS_CHOICE:
3859 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice));
Radek Krejci056d0a82018-12-06 16:57:25 +01003860 node_compile_spec = lys_compile_node_choice;
Radek Krejci19a96102018-11-15 13:38:09 +01003861 break;
Radek Krejci19a96102018-11-15 13:38:09 +01003862 case LYS_ANYXML:
3863 case LYS_ANYDATA:
3864 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata));
Radek Krejci9800fb82018-12-13 14:26:23 +01003865 node_compile_spec = lys_compile_node_any;
Radek Krejci19a96102018-11-15 13:38:09 +01003866 break;
Radek Krejcie86bf772018-12-14 11:39:53 +01003867 case LYS_USES:
3868 return lys_compile_uses(ctx, (struct lysp_node_uses*)node_p, options, parent);
Radek Krejci19a96102018-11-15 13:38:09 +01003869 default:
3870 LOGINT(ctx->ctx);
3871 return LY_EINT;
3872 }
3873 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
3874 node->nodetype = node_p->nodetype;
3875 node->module = ctx->mod;
3876 node->prev = node;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003877 node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01003878
3879 /* config */
3880 if (!(node->flags & LYS_CONFIG_MASK)) {
3881 /* config not explicitely set, inherit it from parent */
3882 if (parent) {
3883 node->flags |= parent->flags & LYS_CONFIG_MASK;
3884 } else {
3885 /* default is config true */
3886 node->flags |= LYS_CONFIG_W;
3887 }
3888 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003889 if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
3890 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3891 "Configuration node cannot be child of any state data node.");
3892 goto error;
3893 }
Radek Krejci19a96102018-11-15 13:38:09 +01003894
Radek Krejcia6d57732018-11-29 13:40:37 +01003895 /* *list ordering */
3896 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
3897 if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) {
3898 LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing state data, "
3899 "RPC/action output parameters or notification content (%s).", ctx->path);
3900 node->flags &= ~LYS_ORDBY_MASK;
3901 node->flags |= LYS_ORDBY_SYSTEM;
3902 } else if (!(node->flags & LYS_ORDBY_MASK)) {
3903 /* default ordering is system */
3904 node->flags |= LYS_ORDBY_SYSTEM;
3905 }
3906 }
3907
Radek Krejci19a96102018-11-15 13:38:09 +01003908 /* status - it is not inherited by specification, but it does not make sense to have
3909 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Radek Krejci056d0a82018-12-06 16:57:25 +01003910 if (!parent || parent->nodetype != LYS_CHOICE) {
3911 /* in case of choice/case's children, postpone the check to the moment we know if
3912 * 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 +01003913 LY_CHECK_GOTO(lys_compile_status(ctx, node, uses_status ? uses_status : (parent ? parent->flags : 0)), error);
Radek Krejci19a96102018-11-15 13:38:09 +01003914 }
3915
3916 if (!(options & LYSC_OPT_FREE_SP)) {
3917 node->sp = node_p;
3918 }
3919 DUP_STRING(ctx->ctx, node_p->name, node->name);
Radek Krejci12fb9142019-01-08 09:45:30 +01003920 DUP_STRING(ctx->ctx, node_p->dsc, node->dsc);
3921 DUP_STRING(ctx->ctx, node_p->ref, node->ref);
Radek Krejci00b874b2019-02-12 10:54:50 +01003922 if (node_p->when) {
3923 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
3924 ret = lys_compile_when(ctx, node_p->when, options, when);
3925 LY_CHECK_GOTO(ret, error);
3926 (*when)->context = lys_compile_xpath_context(node);
3927 }
Radek Krejci9800fb82018-12-13 14:26:23 +01003928 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, node->iffeatures, options, u, lys_compile_iffeature, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01003929 COMPILE_ARRAY_GOTO(ctx, node_p->exts, node->exts, options, u, lys_compile_ext, ret, error);
3930
3931 /* nodetype-specific part */
3932 LY_CHECK_GOTO(node_compile_spec(ctx, node_p, options, node), error);
3933
3934 /* insert into parent's children */
Radek Krejcia3045382018-11-22 14:30:31 +01003935 if (parent) {
3936 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci056d0a82018-12-06 16:57:25 +01003937 cs = lys_compile_node_case(ctx, node_p->parent, options, (struct lysc_node_choice*)parent, node);
3938 LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error);
Radek Krejcib1b59152019-01-07 13:21:56 +01003939 if (uses_status) {
3940
3941 }
Radek Krejci056d0a82018-12-06 16:57:25 +01003942 /* the postponed status check of the node and its real parent - in case of implicit case,
Radek Krejcib1b59152019-01-07 13:21:56 +01003943 * it directly gets the same status flags as the choice;
3944 * uses_status cannot be applied here since uses cannot be child statement of choice */
3945 LY_CHECK_GOTO(lys_compile_status(ctx, node, cs->flags), error);
Radek Krejci056d0a82018-12-06 16:57:25 +01003946 node->parent = (struct lysc_node*)cs;
3947 } else { /* other than choice */
3948 node->parent = parent;
Radek Krejci19a96102018-11-15 13:38:09 +01003949 }
Radek Krejci95710c92019-02-11 15:49:55 +01003950 LY_CHECK_RET(lys_compile_node_connect(ctx, parent->nodetype == LYS_CASE ? parent->parent : parent, node), LY_EVALID);
Radek Krejci19a96102018-11-15 13:38:09 +01003951 } else {
3952 /* top-level element */
3953 if (!ctx->mod->compiled->data) {
3954 ctx->mod->compiled->data = node;
3955 } else {
3956 /* insert at the end of the module's top-level nodes list */
3957 ctx->mod->compiled->data->prev->next = node;
3958 node->prev = ctx->mod->compiled->data->prev;
3959 ctx->mod->compiled->data->prev = node;
3960 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003961 if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs,
3962 ctx->mod->compiled->notifs, node->name, node)) {
3963 return LY_EVALID;
3964 }
Radek Krejci19a96102018-11-15 13:38:09 +01003965 }
3966
3967 return LY_SUCCESS;
3968
3969error:
3970 lysc_node_free(ctx->ctx, node);
3971 return ret;
3972}
3973
Radek Krejcif12a1f02019-02-11 16:42:08 +01003974static void
3975lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result)
3976{
3977 unsigned int v;
3978 size_t len;
3979
3980 len = strlen(aug_p->nodeid);
3981 LY_ARRAY_FOR(result, v) {
3982 if (strlen(result[v]->nodeid) <= len) {
3983 continue;
3984 }
3985 if (v < LY_ARRAY_SIZE(result)) {
3986 /* move the rest of array */
3987 memmove(&result[v + 1], &result[v], (LY_ARRAY_SIZE(result) - v) * sizeof *result);
3988 break;
3989 }
3990 }
3991 result[v] = aug_p;
3992 LY_ARRAY_INCREMENT(result);
3993}
3994
Radek Krejci95710c92019-02-11 15:49:55 +01003995LY_ERR
Radek Krejcif12a1f02019-02-11 16:42:08 +01003996lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_module *mod_p, struct lysp_augment ***augments)
Radek Krejci95710c92019-02-11 15:49:55 +01003997{
3998 struct lysp_augment **result = NULL;
3999 unsigned int u, v;
Radek Krejcif12a1f02019-02-11 16:42:08 +01004000 size_t count = 0;
Radek Krejci95710c92019-02-11 15:49:55 +01004001
Radek Krejcif12a1f02019-02-11 16:42:08 +01004002 assert(mod_p);
Radek Krejci95710c92019-02-11 15:49:55 +01004003 assert(augments);
4004
Radek Krejcif12a1f02019-02-11 16:42:08 +01004005 /* get count of the augments in module and all its submodules */
4006 if (mod_p->augments) {
4007 count += LY_ARRAY_SIZE(mod_p->augments);
4008 }
4009 LY_ARRAY_FOR(mod_p->includes, u) {
4010 if (mod_p->includes[u].submodule->augments) {
4011 count += LY_ARRAY_SIZE(mod_p->includes[u].submodule->augments);
4012 }
4013 }
4014
4015 if (!count) {
Radek Krejci95710c92019-02-11 15:49:55 +01004016 *augments = NULL;
4017 return LY_SUCCESS;
4018 }
Radek Krejcif12a1f02019-02-11 16:42:08 +01004019 LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM);
Radek Krejci95710c92019-02-11 15:49:55 +01004020
4021 /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them
4022 * together, so there can be even /z/y betwwen them. */
Radek Krejcif12a1f02019-02-11 16:42:08 +01004023 LY_ARRAY_FOR(mod_p->augments, u) {
4024 lys_compile_augment_sort_(&mod_p->augments[u], result);
4025 }
4026 LY_ARRAY_FOR(mod_p->includes, u) {
4027 LY_ARRAY_FOR(mod_p->includes[u].submodule->augments, v) {
4028 lys_compile_augment_sort_(&mod_p->includes[u].submodule->augments[v], result);
Radek Krejci95710c92019-02-11 15:49:55 +01004029 }
Radek Krejci95710c92019-02-11 15:49:55 +01004030 }
4031
4032 *augments = result;
4033 return LY_SUCCESS;
4034}
4035
4036/**
4037 * @brief Compile the parsed augment connecting it into its target.
4038 *
4039 * It is expected that all the data referenced in path are present - augments are ordered so that augment B
4040 * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path
4041 * are already implemented and compiled.
4042 *
4043 * @param[in] ctx Compile context.
4044 * @param[in] aug_p Parsed augment to compile.
4045 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
4046 * @param[in] parent Parent node to provide the augment's context. It is NULL for the top level augments and a node holding uses's
4047 * children in case of the augmenting uses data.
4048 * @return LY_SUCCESS on success.
4049 * @return LY_EVALID on failure.
4050 */
4051LY_ERR
4052lys_compile_augment(struct lysc_ctx *ctx, struct lysp_augment *aug_p, int options, const struct lysc_node *parent)
4053{
4054 LY_ERR ret = LY_SUCCESS;
4055 struct lysp_node *node_p, *case_node_p;
4056 struct lysc_node *target; /* target target of the augment */
Radek Krejci00b874b2019-02-12 10:54:50 +01004057 struct lysc_node *node;
4058 struct lysc_node_case *next_case;
4059 struct lysc_when **when, *when_shared;
Radek Krejci95710c92019-02-11 15:49:55 +01004060
4061 ret = lys_resolve_schema_nodeid(ctx, aug_p->nodeid, 0, parent,
4062 LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF,
4063 1, (const struct lysc_node**)&target);
4064 if (ret != LY_SUCCESS) {
4065 if (ret == LY_EDENIED) {
4066 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4067 "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.",
4068 parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype));
4069 }
4070 return LY_EVALID;
4071 }
4072
Radek Krejci00b874b2019-02-12 10:54:50 +01004073 /* check for mandatory nodes
4074 * - new cases augmenting some choice can have mandatory nodes
4075 * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement
4076 */
4077
4078 when_shared = NULL;
Radek Krejci95710c92019-02-11 15:49:55 +01004079 LY_LIST_FOR(aug_p->child, node_p) {
4080 /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */
4081 if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE)
4082 && !((target->nodetype & (LYS_CONTAINER | LYS_LIST)) && (node_p->nodetype & (LYS_ACTION | LYS_NOTIF)))
4083 && !(node_p->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_CHOICE | LYS_LEAF | LYS_LIST | LYS_LEAFLIST | LYS_USES))) {
4084 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4085 "Invalid augment (%s) of %s node which is not allowed to contain %s node \"%s\".",
4086 aug_p->nodeid, lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name);
4087 return LY_EVALID;
4088 }
Radek Krejci00b874b2019-02-12 10:54:50 +01004089
Radek Krejci95710c92019-02-11 15:49:55 +01004090 /* compile the children */
4091 if (node_p->nodetype != LYS_CASE) {
4092 LY_CHECK_RET(lys_compile_node(ctx, node_p, options, target, 0));
4093 } else {
4094 LY_LIST_FOR(((struct lysp_node_case *)node_p)->child, case_node_p) {
4095 LY_CHECK_RET(lys_compile_node(ctx, case_node_p, options, target, 0));
4096 }
4097 }
Radek Krejci00b874b2019-02-12 10:54:50 +01004098
4099 /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children */
4100 if (target->nodetype == LYS_CASE) {
4101 /* the compiled node is the last child of the target (but it is a case, so we have to be careful) */
4102 next_case = target->next ? (struct lysc_node_case*)target->next : ((struct lysc_node_choice*)target->parent)->cases;
4103 for (node = (struct lysc_node*)lysc_node_children(target); node->next && node->next != next_case->child; node = node->next);
4104 } else if (target->nodetype == LYS_CHOICE) {
4105 /* to pass when statement, we need the last case no matter if it is explicit or implicit case */
4106 node = ((struct lysc_node_choice*)target)->cases->prev;
4107 } else {
4108 /* the compiled node is the last child of the target */
4109 node = lysc_node_children(target)->prev;
4110 }
4111
4112 /* pass augment's when to all the children */
4113 if (aug_p->when) {
4114 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
4115 if (!when_shared) {
4116 ret = lys_compile_when(ctx, aug_p->when, options, when);
4117 LY_CHECK_GOTO(ret, error);
4118 (*when)->context = lys_compile_xpath_context(target);
4119 when_shared = *when;
4120 } else {
4121 ++when_shared->refcount;
4122 (*when) = when_shared;
4123 }
4124 }
Radek Krejci95710c92019-02-11 15:49:55 +01004125 }
4126 /* TODO actions, notifications */
4127
Radek Krejci00b874b2019-02-12 10:54:50 +01004128error:
Radek Krejci95710c92019-02-11 15:49:55 +01004129 return ret;
4130}
4131
Radek Krejcia3045382018-11-22 14:30:31 +01004132/**
Radek Krejcid05cbd92018-12-05 14:26:40 +01004133 * @brief Compile the given YANG submodule into the main module.
4134 * @param[in] ctx Compile context
4135 * @param[in] inc Include structure from the main module defining the submodule.
4136 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
4137 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4138 */
4139LY_ERR
4140lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc, int options)
4141{
4142 unsigned int u;
4143 LY_ERR ret = LY_SUCCESS;
4144 /* shortcuts */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004145 struct lysp_submodule *submod = inc->submodule;
Radek Krejcid05cbd92018-12-05 14:26:40 +01004146 struct lysc_module *mainmod = ctx->mod->compiled;
4147
Radek Krejci0af46292019-01-11 16:02:31 +01004148 if (!mainmod->mod->off_features) {
4149 /* features are compiled directly into the compiled module structure,
4150 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves.
4151 * The features compilation is finished in the main module (lys_compile()). */
4152 ret = lys_feature_precompile(ctx->ctx, submod->features,
4153 mainmod->mod->off_features ? &mainmod->mod->off_features : &mainmod->features);
4154 LY_CHECK_GOTO(ret, error);
4155 }
4156
Radek Krejcid05cbd92018-12-05 14:26:40 +01004157 COMPILE_ARRAY_UNIQUE_GOTO(ctx, submod->identities, mainmod->identities, options, u, lys_compile_identity, ret, error);
4158
4159error:
4160 return ret;
4161}
4162
Radek Krejci95710c92019-02-11 15:49:55 +01004163
4164
Radek Krejci19a96102018-11-15 13:38:09 +01004165LY_ERR
4166lys_compile(struct lys_module *mod, int options)
4167{
4168 struct lysc_ctx ctx = {0};
4169 struct lysc_module *mod_c;
Radek Krejci412ddfa2018-11-23 11:44:11 +01004170 struct lysc_type *type, *typeiter;
Radek Krejci19a96102018-11-15 13:38:09 +01004171 struct lysp_module *sp;
4172 struct lysp_node *node_p;
Radek Krejci95710c92019-02-11 15:49:55 +01004173 struct lysp_augment **augments = NULL;
4174 struct lys_module *m;
Radek Krejcicdfecd92018-11-26 11:27:32 +01004175 unsigned int u, v;
Radek Krejcid05cbd92018-12-05 14:26:40 +01004176 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01004177
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004178 LY_CHECK_ARG_RET(NULL, mod, mod->parsed, mod->ctx, LY_EINVAL);
Radek Krejci096235c2019-01-11 11:12:19 +01004179
4180 if (!mod->implemented) {
4181 /* just imported modules are not compiled */
4182 return LY_SUCCESS;
4183 }
4184
Radek Krejci19a96102018-11-15 13:38:09 +01004185 sp = mod->parsed;
4186
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004187 ctx.ctx = mod->ctx;
Radek Krejci19a96102018-11-15 13:38:09 +01004188 ctx.mod = mod;
Radek Krejcie86bf772018-12-14 11:39:53 +01004189 ctx.mod_def = mod;
Radek Krejci19a96102018-11-15 13:38:09 +01004190
4191 mod->compiled = mod_c = calloc(1, sizeof *mod_c);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004192 LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM);
4193 mod_c->mod = mod;
Radek Krejci19a96102018-11-15 13:38:09 +01004194
Radek Krejci19a96102018-11-15 13:38:09 +01004195 COMPILE_ARRAY_GOTO(&ctx, sp->imports, mod_c->imports, options, u, lys_compile_import, ret, error);
Radek Krejcid05cbd92018-12-05 14:26:40 +01004196 LY_ARRAY_FOR(sp->includes, u) {
4197 ret = lys_compile_submodule(&ctx, &sp->includes[u], options);
4198 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
4199 }
Radek Krejci0af46292019-01-11 16:02:31 +01004200 if (mod->off_features) {
4201 /* there is already precompiled array of features */
4202 mod_c->features = mod->off_features;
4203 mod->off_features = NULL;
Radek Krejci0af46292019-01-11 16:02:31 +01004204 } else {
4205 /* features are compiled directly into the compiled module structure,
4206 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves */
4207 ret = lys_feature_precompile(ctx.ctx, sp->features, &mod_c->features);
4208 LY_CHECK_GOTO(ret, error);
4209 }
4210 /* finish feature compilation, not only for the main module, but also for the submodules.
4211 * Due to possible forward references, it must be done when all the features (including submodules)
4212 * are present. */
4213 LY_ARRAY_FOR(sp->features, u) {
4214 ret = lys_feature_precompile_finish(&ctx, &sp->features[u], options, mod_c->features);
4215 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
4216 }
4217 LY_ARRAY_FOR(sp->includes, v) {
4218 LY_ARRAY_FOR(sp->includes[v].submodule->features, u) {
4219 ret = lys_feature_precompile_finish(&ctx, &sp->includes[v].submodule->features[u], options, mod_c->features);
4220 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
4221 }
4222 }
4223
Radek Krejcid05cbd92018-12-05 14:26:40 +01004224 COMPILE_ARRAY_UNIQUE_GOTO(&ctx, sp->identities, mod_c->identities, options, u, lys_compile_identity, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01004225 if (sp->identities) {
4226 LY_CHECK_RET(lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities));
4227 }
4228
Radek Krejci95710c92019-02-11 15:49:55 +01004229 /* data nodes */
Radek Krejci19a96102018-11-15 13:38:09 +01004230 LY_LIST_FOR(sp->data, node_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01004231 ret = lys_compile_node(&ctx, node_p, options, NULL, 0);
Radek Krejci19a96102018-11-15 13:38:09 +01004232 LY_CHECK_GOTO(ret, error);
4233 }
Radek Krejci95710c92019-02-11 15:49:55 +01004234
4235 /* augments - sort first to cover augments augmenting other augments */
Radek Krejcif12a1f02019-02-11 16:42:08 +01004236 ret = lys_compile_augment_sort(&ctx, sp, &augments);
Radek Krejci95710c92019-02-11 15:49:55 +01004237 LY_CHECK_GOTO(ret, error);
4238 LY_ARRAY_FOR(augments, u) {
4239 ret = lys_compile_augment(&ctx, augments[u], options, NULL);
4240 LY_CHECK_GOTO(ret, error);
4241 }
Radek Krejci19a96102018-11-15 13:38:09 +01004242 //COMPILE_ARRAY_GOTO(ctx, sp->rpcs, mod_c->rpcs, options, u, lys_compile_action, ret, error);
4243 //COMPILE_ARRAY_GOTO(ctx, sp->notifs, mod_c->notifs, options, u, lys_compile_notif, ret, error);
4244
4245 COMPILE_ARRAY_GOTO(&ctx, sp->exts, mod_c->exts, options, u, lys_compile_ext, ret, error);
4246
Radek Krejcia3045382018-11-22 14:30:31 +01004247 /* validate leafref's paths and when/must xpaths */
Radek Krejci412ddfa2018-11-23 11:44:11 +01004248 /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which
4249 * can be also leafref, in case it is already resolved, go through the chain and check that it does not
4250 * 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 +01004251 for (u = 0; u < ctx.unres.count; ++u) {
Radek Krejci0e5d8382018-11-28 16:37:53 +01004252 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Radek Krejcia3045382018-11-22 14:30:31 +01004253 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
4254 if (type->basetype == LY_TYPE_LEAFREF) {
4255 /* validate the path */
Radek Krejci412ddfa2018-11-23 11:44:11 +01004256 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 +01004257 LY_CHECK_GOTO(ret, error);
Radek Krejcicdfecd92018-11-26 11:27:32 +01004258 } else if (type->basetype == LY_TYPE_UNION) {
4259 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
4260 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
4261 /* validate the path */
4262 ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[u]),
4263 (struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v]);
4264 LY_CHECK_GOTO(ret, error);
4265 }
4266 }
Radek Krejcia3045382018-11-22 14:30:31 +01004267 }
4268 }
4269 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01004270 for (u = 0; u < ctx.unres.count; ++u) {
Radek Krejci0e5d8382018-11-28 16:37:53 +01004271 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Radek Krejci412ddfa2018-11-23 11:44:11 +01004272 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
4273 if (type->basetype == LY_TYPE_LEAFREF) {
4274 /* store pointer to the real type */
4275 for (typeiter = ((struct lysc_type_leafref*)type)->realtype;
4276 typeiter->basetype == LY_TYPE_LEAFREF;
4277 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
4278 ((struct lysc_type_leafref*)type)->realtype = typeiter;
Radek Krejcicdfecd92018-11-26 11:27:32 +01004279 } else if (type->basetype == LY_TYPE_UNION) {
4280 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
4281 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
4282 /* store pointer to the real type */
4283 for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype;
4284 typeiter->basetype == LY_TYPE_LEAFREF;
4285 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
4286 ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter;
4287 }
4288 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01004289 }
4290 }
4291 }
Radek Krejcia3045382018-11-22 14:30:31 +01004292 ly_set_erase(&ctx.unres, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01004293 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01004294 LY_ARRAY_FREE(augments);
Radek Krejcia3045382018-11-22 14:30:31 +01004295
Radek Krejci19a96102018-11-15 13:38:09 +01004296 if (options & LYSC_OPT_FREE_SP) {
4297 lysp_module_free(mod->parsed);
4298 ((struct lys_module*)mod)->parsed = NULL;
4299 }
4300
Radek Krejci95710c92019-02-11 15:49:55 +01004301 if (!(options & LYSC_OPT_INTERNAL)) {
4302 /* remove flag of the modules implemented by dependency */
4303 for (u = 0; u < ctx.ctx->list.count; ++u) {
4304 m = ctx.ctx->list.objs[u];
4305 if (m->implemented == 2) {
4306 m->implemented = 1;
4307 }
4308 }
4309 }
4310
Radek Krejci19a96102018-11-15 13:38:09 +01004311 ((struct lys_module*)mod)->compiled = mod_c;
4312 return LY_SUCCESS;
4313
4314error:
Radek Krejci95710c92019-02-11 15:49:55 +01004315 lys_feature_precompile_revert(&ctx, mod);
Radek Krejcia3045382018-11-22 14:30:31 +01004316 ly_set_erase(&ctx.unres, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01004317 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01004318 LY_ARRAY_FREE(augments);
Radek Krejci19a96102018-11-15 13:38:09 +01004319 lysc_module_free(mod_c, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01004320 mod->compiled = NULL;
4321
4322 /* revert compilation of modules implemented by dependency */
4323 for (u = 0; u < ctx.ctx->list.count; ++u) {
4324 m = ctx.ctx->list.objs[u];
4325 if (m->implemented == 2) {
4326 /* revert features list to the precompiled state */
4327 lys_feature_precompile_revert(&ctx, m);
4328 /* mark module as imported-only / not-implemented */
4329 m->implemented = 0;
4330 /* free the compiled version of the module */
4331 lysc_module_free(m->compiled, NULL);
4332 m->compiled = NULL;
4333 }
4334 }
4335
Radek Krejci19a96102018-11-15 13:38:09 +01004336 return ret;
4337}