blob: 1f57db9354bbf11c673d048e11f9a249751f18c0 [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
Radek Krejciec4da802019-05-02 13:02:41 +020033#define COMPILE_ARRAY_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, FUNC, RET, GOTO) \
Radek Krejci19a96102018-11-15 13:38:09 +010034 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 Krejciec4da802019-05-02 13:02:41 +020039 RET = FUNC(CTX, &(ARRAY_P)[ITER], &(ARRAY_C)[ITER + __array_offset]); \
Radek Krejcid05cbd92018-12-05 14:26:40 +010040 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
41 } \
42 }
43
Radek Krejciec4da802019-05-02 13:02:41 +020044#define COMPILE_ARRAY1_GOTO(CTX, ARRAY_P, ARRAY_C, PARENT, ITER, FUNC, USES_STATUS, RET, GOTO) \
Radek Krejci6eeb58f2019-02-22 16:29:37 +010045 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); \
Radek Krejciec4da802019-05-02 13:02:41 +020050 RET = FUNC(CTX, &(ARRAY_P)[ITER], PARENT, &(ARRAY_C)[ITER + __array_offset], USES_STATUS); \
Radek Krejci6eeb58f2019-02-22 16:29:37 +010051 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
52 } \
53 }
54
Radek Krejciec4da802019-05-02 13:02:41 +020055#define COMPILE_ARRAY_UNIQUE_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, FUNC, RET, GOTO) \
Radek Krejcid05cbd92018-12-05 14:26:40 +010056 if (ARRAY_P) { \
57 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \
58 size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
59 for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \
60 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020061 RET = FUNC(CTX, &(ARRAY_P)[ITER], ARRAY_C, &(ARRAY_C)[ITER + __array_offset]); \
Radek Krejci19a96102018-11-15 13:38:09 +010062 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
63 } \
64 }
65
Radek Krejciec4da802019-05-02 13:02:41 +020066#define COMPILE_MEMBER_GOTO(CTX, MEMBER_P, MEMBER_C, FUNC, RET, GOTO) \
Radek Krejci19a96102018-11-15 13:38:09 +010067 if (MEMBER_P) { \
68 MEMBER_C = calloc(1, sizeof *(MEMBER_C)); \
69 LY_CHECK_ERR_GOTO(!(MEMBER_C), LOGMEM((CTX)->ctx); RET = LY_EMEM, GOTO); \
Radek Krejciec4da802019-05-02 13:02:41 +020070 RET = FUNC(CTX, MEMBER_P, MEMBER_C); \
Radek Krejci19a96102018-11-15 13:38:09 +010071 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
72 }
73
Radek Krejciec4da802019-05-02 13:02:41 +020074#define COMPILE_MEMBER_ARRAY_GOTO(CTX, MEMBER_P, ARRAY_C, FUNC, RET, GOTO) \
Radek Krejci00b874b2019-02-12 10:54:50 +010075 if (MEMBER_P) { \
76 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, 1, RET, GOTO); \
77 size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
78 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020079 RET = FUNC(CTX, MEMBER_P, &(ARRAY_C)[__array_offset]); \
Radek Krejci00b874b2019-02-12 10:54:50 +010080 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
81 }
82
Radek Krejcid05cbd92018-12-05 14:26:40 +010083#define COMPILE_CHECK_UNIQUENESS(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \
84 if (ARRAY) { \
Radek Krejci0af46292019-01-11 16:02:31 +010085 for (unsigned int u__ = 0; u__ < LY_ARRAY_SIZE(ARRAY); ++u__) { \
86 if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__].MEMBER) == (void*)(IDENT)) { \
Radek Krejcid05cbd92018-12-05 14:26:40 +010087 LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \
88 return LY_EVALID; \
89 } \
90 } \
91 }
92
Radek Krejci19a96102018-11-15 13:38:09 +010093static struct lysc_ext_instance *
94lysc_ext_instance_dup(struct ly_ctx *ctx, struct lysc_ext_instance *orig)
95{
96 /* TODO */
97 (void) ctx;
98 (void) orig;
99 return NULL;
100}
101
Radek Krejcib56c7502019-02-13 14:19:54 +0100102/**
103 * @brief Duplicate the compiled pattern structure.
104 *
105 * Instead of duplicating memory, the reference counter in the @p orig is increased.
106 *
107 * @param[in] orig The pattern structure to duplicate.
108 * @return The duplicated structure to use.
109 */
Radek Krejci19a96102018-11-15 13:38:09 +0100110static struct lysc_pattern*
111lysc_pattern_dup(struct lysc_pattern *orig)
112{
113 ++orig->refcount;
114 return orig;
115}
116
Radek Krejcib56c7502019-02-13 14:19:54 +0100117/**
118 * @brief Duplicate the array of compiled patterns.
119 *
120 * The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter.
121 *
122 * @param[in] ctx Libyang context for logging.
123 * @param[in] orig The patterns sized array to duplicate.
124 * @return New sized array as a copy of @p orig.
125 * @return NULL in case of memory allocation error.
126 */
Radek Krejci19a96102018-11-15 13:38:09 +0100127static struct lysc_pattern**
128lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig)
129{
Radek Krejcid05cbd92018-12-05 14:26:40 +0100130 struct lysc_pattern **dup = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100131 unsigned int u;
132
Radek Krejcib56c7502019-02-13 14:19:54 +0100133 assert(orig);
134
Radek Krejci19a96102018-11-15 13:38:09 +0100135 LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_SIZE(orig), NULL);
136 LY_ARRAY_FOR(orig, u) {
137 dup[u] = lysc_pattern_dup(orig[u]);
138 LY_ARRAY_INCREMENT(dup);
139 }
140 return dup;
141}
142
Radek Krejcib56c7502019-02-13 14:19:54 +0100143/**
144 * @brief Duplicate compiled range structure.
145 *
146 * @param[in] ctx Libyang context for logging.
147 * @param[in] orig The range structure to be duplicated.
148 * @return New compiled range structure as a copy of @p orig.
149 * @return NULL in case of memory allocation error.
150 */
Radek Krejci19a96102018-11-15 13:38:09 +0100151struct lysc_range*
152lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig)
153{
154 struct lysc_range *dup;
155 LY_ERR ret;
156
Radek Krejcib56c7502019-02-13 14:19:54 +0100157 assert(orig);
158
Radek Krejci19a96102018-11-15 13:38:09 +0100159 dup = calloc(1, sizeof *dup);
160 LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL);
161 if (orig->parts) {
162 LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_SIZE(orig->parts), ret, cleanup);
163 LY_ARRAY_SIZE(dup->parts) = LY_ARRAY_SIZE(orig->parts);
164 memcpy(dup->parts, orig->parts, LY_ARRAY_SIZE(dup->parts) * sizeof *dup->parts);
165 }
166 DUP_STRING(ctx, orig->eapptag, dup->eapptag);
167 DUP_STRING(ctx, orig->emsg, dup->emsg);
168 dup->exts = lysc_ext_instance_dup(ctx, orig->exts);
169
170 return dup;
171cleanup:
172 free(dup);
173 (void) ret; /* set but not used due to the return type */
174 return NULL;
175}
176
Radek Krejcib56c7502019-02-13 14:19:54 +0100177/**
178 * @brief Stack for processing if-feature expressions.
179 */
Radek Krejci19a96102018-11-15 13:38:09 +0100180struct iff_stack {
Radek Krejcib56c7502019-02-13 14:19:54 +0100181 int size; /**< number of items in the stack */
182 int index; /**< first empty item */
183 uint8_t *stack;/**< stack - array of @ref ifftokens to create the if-feature expression in prefix format */
Radek Krejci19a96102018-11-15 13:38:09 +0100184};
185
Radek Krejcib56c7502019-02-13 14:19:54 +0100186/**
187 * @brief Add @ref ifftokens into the stack.
188 * @param[in] stack The if-feature stack to use.
189 * @param[in] value One of the @ref ifftokens to store in the stack.
190 * @return LY_EMEM in case of memory allocation error
191 * @return LY_ESUCCESS if the value successfully stored.
192 */
Radek Krejci19a96102018-11-15 13:38:09 +0100193static LY_ERR
194iff_stack_push(struct iff_stack *stack, uint8_t value)
195{
196 if (stack->index == stack->size) {
197 stack->size += 4;
198 stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack);
199 LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM);
200 }
201 stack->stack[stack->index++] = value;
202 return LY_SUCCESS;
203}
204
Radek Krejcib56c7502019-02-13 14:19:54 +0100205/**
206 * @brief Get (and remove) the last item form the stack.
207 * @param[in] stack The if-feature stack to use.
208 * @return The value from the top of the stack.
209 */
Radek Krejci19a96102018-11-15 13:38:09 +0100210static uint8_t
211iff_stack_pop(struct iff_stack *stack)
212{
Radek Krejcib56c7502019-02-13 14:19:54 +0100213 assert(stack && stack->index);
214
Radek Krejci19a96102018-11-15 13:38:09 +0100215 stack->index--;
216 return stack->stack[stack->index];
217}
218
Radek Krejcib56c7502019-02-13 14:19:54 +0100219/**
220 * @brief Clean up the stack.
221 * @param[in] stack The if-feature stack to use.
222 */
Radek Krejci19a96102018-11-15 13:38:09 +0100223static void
224iff_stack_clean(struct iff_stack *stack)
225{
226 stack->size = 0;
227 free(stack->stack);
228}
229
Radek Krejcib56c7502019-02-13 14:19:54 +0100230/**
231 * @brief Store the @ref ifftokens (@p op) on the given position in the 2bits array
232 * (libyang format of the if-feature expression).
233 * @param[in,out] list The 2bits array to modify.
234 * @param[in] op The operand (@ref ifftokens) to store.
235 * @param[in] pos Position (0-based) where to store the given @p op.
236 */
Radek Krejci19a96102018-11-15 13:38:09 +0100237static void
238iff_setop(uint8_t *list, uint8_t op, int pos)
239{
240 uint8_t *item;
241 uint8_t mask = 3;
242
243 assert(pos >= 0);
244 assert(op <= 3); /* max 2 bits */
245
246 item = &list[pos / 4];
247 mask = mask << 2 * (pos % 4);
248 *item = (*item) & ~mask;
249 *item = (*item) | (op << 2 * (pos % 4));
250}
251
Radek Krejcib56c7502019-02-13 14:19:54 +0100252#define LYS_IFF_LP 0x04 /**< Additional, temporary, value of @ref ifftokens: ( */
253#define LYS_IFF_RP 0x08 /**< Additional, temporary, value of @ref ifftokens: ) */
Radek Krejci19a96102018-11-15 13:38:09 +0100254
Radek Krejci0af46292019-01-11 16:02:31 +0100255/**
256 * @brief Find a feature of the given name and referenced in the given module.
257 *
258 * If the compiled schema is available (the schema is implemented), the feature from the compiled schema is
259 * returned. Otherwise, the special array of pre-compiled features is used to search for the feature. Such
260 * features are always disabled (feature from not implemented schema cannot be enabled), but in case the schema
261 * will be made implemented in future (no matter if implicitly via augmenting/deviating it or explicitly via
262 * ly_ctx_module_implement()), the compilation of these feature structure is finished, but the pointers
263 * assigned till that time will be still valid.
264 *
265 * @param[in] mod Module where the feature was referenced (used to resolve prefix of the feature).
266 * @param[in] name Name of the feature including possible prefix.
267 * @param[in] len Length of the string representing the feature identifier in the name variable (mandatory!).
268 * @return Pointer to the feature structure if found, NULL otherwise.
269 */
Radek Krejci19a96102018-11-15 13:38:09 +0100270static struct lysc_feature *
Radek Krejci0af46292019-01-11 16:02:31 +0100271lys_feature_find(struct lys_module *mod, const char *name, size_t len)
Radek Krejci19a96102018-11-15 13:38:09 +0100272{
273 size_t i;
Radek Krejci0af46292019-01-11 16:02:31 +0100274 struct lysc_feature *f, *flist;
Radek Krejci19a96102018-11-15 13:38:09 +0100275
276 for (i = 0; i < len; ++i) {
277 if (name[i] == ':') {
278 /* we have a prefixed feature */
Radek Krejci0af46292019-01-11 16:02:31 +0100279 mod = lys_module_find_prefix(mod, name, i);
Radek Krejci19a96102018-11-15 13:38:09 +0100280 LY_CHECK_RET(!mod, NULL);
281
282 name = &name[i + 1];
283 len = len - i - 1;
284 }
285 }
286
287 /* we have the correct module, get the feature */
Radek Krejci0af46292019-01-11 16:02:31 +0100288 if (mod->implemented) {
289 /* module is implemented so there is already the compiled schema */
290 flist = mod->compiled->features;
291 } else {
292 flist = mod->off_features;
293 }
294 LY_ARRAY_FOR(flist, i) {
295 f = &flist[i];
Radek Krejci19a96102018-11-15 13:38:09 +0100296 if (!strncmp(f->name, name, len) && f->name[len] == '\0') {
297 return f;
298 }
299 }
300
301 return NULL;
302}
303
304static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200305lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext)
Radek Krejci19a96102018-11-15 13:38:09 +0100306{
307 const char *name;
308 unsigned int u;
309 const struct lys_module *mod;
310 struct lysp_ext *edef = NULL;
311
312 DUP_STRING(ctx->ctx, ext_p->argument, ext->argument);
313 ext->insubstmt = ext_p->insubstmt;
314 ext->insubstmt_index = ext_p->insubstmt_index;
315
316 /* get module where the extension definition should be placed */
317 for (u = 0; ext_p->name[u] != ':'; ++u);
Radek Krejcie86bf772018-12-14 11:39:53 +0100318 mod = lys_module_find_prefix(ctx->mod_def, ext_p->name, u);
Radek Krejci19a96102018-11-15 13:38:09 +0100319 LY_CHECK_ERR_RET(!mod, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
320 "Invalid prefix \"%.*s\" used for extension instance identifier.", u, ext_p->name),
321 LY_EVALID);
322 LY_CHECK_ERR_RET(!mod->parsed->extensions,
323 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
324 "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100325 ext_p->name, mod->name),
Radek Krejci19a96102018-11-15 13:38:09 +0100326 LY_EVALID);
327 name = &ext_p->name[u + 1];
328 /* find the extension definition there */
329 for (ext = NULL, u = 0; u < LY_ARRAY_SIZE(mod->parsed->extensions); ++u) {
330 if (!strcmp(name, mod->parsed->extensions[u].name)) {
331 edef = &mod->parsed->extensions[u];
332 break;
333 }
334 }
335 LY_CHECK_ERR_RET(!edef, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
336 "Extension definition of extension instance \"%s\" not found.", ext_p->name),
337 LY_EVALID);
338 /* TODO plugins */
339
340 return LY_SUCCESS;
341}
342
Radek Krejcib56c7502019-02-13 14:19:54 +0100343/**
344 * @brief Compile information from the if-feature statement
345 * @param[in] ctx Compile context.
346 * @param[in] value The if-feature argument to process. It is pointer-to-pointer-to-char just to unify the compile functions.
Radek Krejcib56c7502019-02-13 14:19:54 +0100347 * @param[in,out] iff Prepared (empty) compiled if-feature structure to fill.
348 * @return LY_ERR value.
349 */
Radek Krejci19a96102018-11-15 13:38:09 +0100350static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200351lys_compile_iffeature(struct lysc_ctx *ctx, const char **value, struct lysc_iffeature *iff)
Radek Krejci19a96102018-11-15 13:38:09 +0100352{
353 const char *c = *value;
354 int r, rc = EXIT_FAILURE;
355 int i, j, last_not, checkversion = 0;
356 unsigned int f_size = 0, expr_size = 0, f_exp = 1;
357 uint8_t op;
358 struct iff_stack stack = {0, 0, NULL};
359 struct lysc_feature *f;
360
361 assert(c);
362
363 /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */
364 for (i = j = last_not = 0; c[i]; i++) {
365 if (c[i] == '(') {
366 j++;
367 checkversion = 1;
368 continue;
369 } else if (c[i] == ')') {
370 j--;
371 continue;
372 } else if (isspace(c[i])) {
373 checkversion = 1;
374 continue;
375 }
376
377 if (!strncmp(&c[i], "not", r = 3) || !strncmp(&c[i], "and", r = 3) || !strncmp(&c[i], "or", r = 2)) {
378 if (c[i + r] == '\0') {
379 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
380 "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value);
381 return LY_EVALID;
382 } else if (!isspace(c[i + r])) {
383 /* feature name starting with the not/and/or */
384 last_not = 0;
385 f_size++;
386 } else if (c[i] == 'n') { /* not operation */
387 if (last_not) {
388 /* double not */
389 expr_size = expr_size - 2;
390 last_not = 0;
391 } else {
392 last_not = 1;
393 }
394 } else { /* and, or */
395 f_exp++;
396 /* not a not operation */
397 last_not = 0;
398 }
399 i += r;
400 } else {
401 f_size++;
402 last_not = 0;
403 }
404 expr_size++;
405
406 while (!isspace(c[i])) {
407 if (!c[i] || c[i] == ')') {
408 i--;
409 break;
410 }
411 i++;
412 }
413 }
414 if (j || f_exp != f_size) {
415 /* not matching count of ( and ) */
416 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
417 "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value);
418 return LY_EVALID;
419 }
420
421 if (checkversion || expr_size > 1) {
422 /* check that we have 1.1 module */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100423 if (ctx->mod_def->version != LYS_VERSION_1_1) {
Radek Krejci19a96102018-11-15 13:38:09 +0100424 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
425 "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value);
426 return LY_EVALID;
427 }
428 }
429
430 /* allocate the memory */
431 LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM);
432 iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr);
433 stack.stack = malloc(expr_size * sizeof *stack.stack);
434 LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx), error);
435
436 stack.size = expr_size;
437 f_size--; expr_size--; /* used as indexes from now */
438
439 for (i--; i >= 0; i--) {
440 if (c[i] == ')') {
441 /* push it on stack */
442 iff_stack_push(&stack, LYS_IFF_RP);
443 continue;
444 } else if (c[i] == '(') {
445 /* pop from the stack into result all operators until ) */
446 while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) {
447 iff_setop(iff->expr, op, expr_size--);
448 }
449 continue;
450 } else if (isspace(c[i])) {
451 continue;
452 }
453
454 /* end of operator or operand -> find beginning and get what is it */
455 j = i + 1;
456 while (i >= 0 && !isspace(c[i]) && c[i] != '(') {
457 i--;
458 }
459 i++; /* go back by one step */
460
461 if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) {
462 if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) {
463 /* double not */
464 iff_stack_pop(&stack);
465 } else {
466 /* not has the highest priority, so do not pop from the stack
467 * as in case of AND and OR */
468 iff_stack_push(&stack, LYS_IFF_NOT);
469 }
470 } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) {
471 /* as for OR - pop from the stack all operators with the same or higher
472 * priority and store them to the result, then push the AND to the stack */
473 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) {
474 op = iff_stack_pop(&stack);
475 iff_setop(iff->expr, op, expr_size--);
476 }
477 iff_stack_push(&stack, LYS_IFF_AND);
478 } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) {
479 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) {
480 op = iff_stack_pop(&stack);
481 iff_setop(iff->expr, op, expr_size--);
482 }
483 iff_stack_push(&stack, LYS_IFF_OR);
484 } else {
485 /* feature name, length is j - i */
486
487 /* add it to the expression */
488 iff_setop(iff->expr, LYS_IFF_F, expr_size--);
489
490 /* now get the link to the feature definition */
Radek Krejci0af46292019-01-11 16:02:31 +0100491 f = lys_feature_find(ctx->mod_def, &c[i], j - i);
492 LY_CHECK_ERR_GOTO(!f, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
493 "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]);
494 rc = LY_EVALID, error)
Radek Krejci19a96102018-11-15 13:38:09 +0100495 iff->features[f_size] = f;
496 LY_ARRAY_INCREMENT(iff->features);
497 f_size--;
498 }
499 }
500 while (stack.index) {
501 op = iff_stack_pop(&stack);
502 iff_setop(iff->expr, op, expr_size--);
503 }
504
505 if (++expr_size || ++f_size) {
506 /* not all expected operators and operands found */
507 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
508 "Invalid value \"%s\" of if-feature - processing error.", *value);
509 rc = LY_EINT;
510 } else {
511 rc = LY_SUCCESS;
512 }
513
514error:
515 /* cleanup */
516 iff_stack_clean(&stack);
517
518 return rc;
519}
520
Radek Krejcib56c7502019-02-13 14:19:54 +0100521/**
522 * @brief Compile information from the when statement
523 * @param[in] ctx Compile context.
524 * @param[in] when_p The parsed when statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100525 * @param[out] when Pointer where to store pointer to the created compiled when structure.
526 * @return LY_ERR value.
527 */
Radek Krejci19a96102018-11-15 13:38:09 +0100528static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200529lys_compile_when(struct lysc_ctx *ctx, struct lysp_when *when_p, struct lysc_when **when)
Radek Krejci19a96102018-11-15 13:38:09 +0100530{
531 unsigned int u;
532 LY_ERR ret = LY_SUCCESS;
533
Radek Krejci00b874b2019-02-12 10:54:50 +0100534 *when = calloc(1, sizeof **when);
535 (*when)->refcount = 1;
536 (*when)->cond = lyxp_expr_parse(ctx->ctx, when_p->cond);
537 DUP_STRING(ctx->ctx, when_p->dsc, (*when)->dsc);
538 DUP_STRING(ctx->ctx, when_p->ref, (*when)->ref);
539 LY_CHECK_ERR_GOTO(!(*when)->cond, ret = ly_errcode(ctx->ctx), done);
Radek Krejciec4da802019-05-02 13:02:41 +0200540 COMPILE_ARRAY_GOTO(ctx, when_p->exts, (*when)->exts, u, lys_compile_ext, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100541
542done:
543 return ret;
544}
545
Radek Krejcib56c7502019-02-13 14:19:54 +0100546/**
547 * @brief Compile information from the must statement
548 * @param[in] ctx Compile context.
549 * @param[in] must_p The parsed must statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100550 * @param[in,out] must Prepared (empty) compiled must structure to fill.
551 * @return LY_ERR value.
552 */
Radek Krejci19a96102018-11-15 13:38:09 +0100553static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200554lys_compile_must(struct lysc_ctx *ctx, struct lysp_restr *must_p, struct lysc_must *must)
Radek Krejci19a96102018-11-15 13:38:09 +0100555{
556 unsigned int u;
557 LY_ERR ret = LY_SUCCESS;
558
559 must->cond = lyxp_expr_parse(ctx->ctx, must_p->arg);
560 LY_CHECK_ERR_GOTO(!must->cond, ret = ly_errcode(ctx->ctx), done);
561
562 DUP_STRING(ctx->ctx, must_p->eapptag, must->eapptag);
563 DUP_STRING(ctx->ctx, must_p->emsg, must->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +0100564 DUP_STRING(ctx->ctx, must_p->dsc, must->dsc);
565 DUP_STRING(ctx->ctx, must_p->ref, must->ref);
Radek Krejciec4da802019-05-02 13:02:41 +0200566 COMPILE_ARRAY_GOTO(ctx, must_p->exts, must->exts, u, lys_compile_ext, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100567
568done:
569 return ret;
570}
571
Radek Krejcib56c7502019-02-13 14:19:54 +0100572/**
573 * @brief Compile information from the import statement
574 * @param[in] ctx Compile context.
575 * @param[in] imp_p The parsed import statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100576 * @param[in,out] imp Prepared (empty) compiled import structure to fill.
577 * @return LY_ERR value.
578 */
Radek Krejci19a96102018-11-15 13:38:09 +0100579static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200580lys_compile_import(struct lysc_ctx *ctx, struct lysp_import *imp_p, struct lysc_import *imp)
Radek Krejci19a96102018-11-15 13:38:09 +0100581{
582 unsigned int u;
583 struct lys_module *mod = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100584 LY_ERR ret = LY_SUCCESS;
585
586 DUP_STRING(ctx->ctx, imp_p->prefix, imp->prefix);
Radek Krejciec4da802019-05-02 13:02:41 +0200587 COMPILE_ARRAY_GOTO(ctx, imp_p->exts, imp->exts, u, lys_compile_ext, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100588 imp->module = imp_p->module;
589
Radek Krejci7f2a5362018-11-28 13:05:37 +0100590 /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs.
591 * The compiled version is needed only for augments, deviates and leafrefs, so they are checked (and added,
Radek Krejci0e5d8382018-11-28 16:37:53 +0100592 * if needed) when these nodes are finally being instantiated and validated at the end of schema compilation. */
Radek Krejci19a96102018-11-15 13:38:09 +0100593 if (!imp->module->parsed) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100594 /* try to use filepath if present */
595 if (imp->module->filepath) {
596 mod = (struct lys_module*)lys_parse_path(ctx->ctx, imp->module->filepath,
597 !strcmp(&imp->module->filepath[strlen(imp->module->filepath - 4)], ".yin") ? LYS_IN_YIN : LYS_IN_YANG);
Radek Krejci19a96102018-11-15 13:38:09 +0100598 if (mod != imp->module) {
599 LOGERR(ctx->ctx, LY_EINT, "Filepath \"%s\" of the module \"%s\" does not match.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100600 imp->module->filepath, imp->module->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100601 mod = NULL;
602 }
603 }
604 if (!mod) {
Radek Krejci0af46292019-01-11 16:02:31 +0100605 if (lysp_load_module(ctx->ctx, imp->module->name, imp->module->revision, 0, 1, &mod)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100606 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 +0100607 imp->module->name, ctx->mod->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100608 return LY_ENOTFOUND;
609 }
610 }
Radek Krejci19a96102018-11-15 13:38:09 +0100611 }
612
613done:
614 return ret;
615}
616
Radek Krejcib56c7502019-02-13 14:19:54 +0100617/**
618 * @brief Compile information from the identity statement
619 *
620 * The backlinks to the identities derived from this one are supposed to be filled later via lys_compile_identity_bases().
621 *
622 * @param[in] ctx Compile context.
623 * @param[in] ident_p The parsed identity statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100624 * @param[in] idents List of so far compiled identities to check the name uniqueness.
625 * @param[in,out] ident Prepared (empty) compiled identity structure to fill.
626 * @return LY_ERR value.
627 */
Radek Krejci19a96102018-11-15 13:38:09 +0100628static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200629lys_compile_identity(struct lysc_ctx *ctx, struct lysp_ident *ident_p, struct lysc_ident *idents, struct lysc_ident *ident)
Radek Krejci19a96102018-11-15 13:38:09 +0100630{
631 unsigned int u;
632 LY_ERR ret = LY_SUCCESS;
633
Radek Krejcid05cbd92018-12-05 14:26:40 +0100634 COMPILE_CHECK_UNIQUENESS(ctx, idents, name, ident, "identity", ident_p->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100635 DUP_STRING(ctx->ctx, ident_p->name, ident->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200636 DUP_STRING(ctx->ctx, ident_p->dsc, ident->dsc);
637 DUP_STRING(ctx->ctx, ident_p->ref, ident->ref);
Radek Krejci693262f2019-04-29 15:23:20 +0200638 ident->module = ctx->mod;
Radek Krejciec4da802019-05-02 13:02:41 +0200639 COMPILE_ARRAY_GOTO(ctx, ident_p->iffeatures, ident->iffeatures, u, lys_compile_iffeature, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100640 /* backlings (derived) can be added no sooner than when all the identities in the current module are present */
Radek Krejciec4da802019-05-02 13:02:41 +0200641 COMPILE_ARRAY_GOTO(ctx, ident_p->exts, ident->exts, u, lys_compile_ext, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100642 ident->flags = ident_p->flags;
643
644done:
645 return ret;
646}
647
Radek Krejcib56c7502019-02-13 14:19:54 +0100648/**
649 * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement).
650 *
651 * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages.
652 *
653 * @param[in] ctx Compile context for logging.
654 * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed).
655 * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident)
656 * @return LY_SUCCESS if everything is ok.
657 * @return LY_EVALID if the identity is derived from itself.
658 */
Radek Krejci38222632019-02-12 16:55:05 +0100659static LY_ERR
660lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived)
661{
662 LY_ERR ret = LY_EVALID;
663 unsigned int u, v;
664 struct ly_set recursion = {0};
665 struct lysc_ident *drv;
666
667 if (!derived) {
668 return LY_SUCCESS;
669 }
670
671 for (u = 0; u < LY_ARRAY_SIZE(derived); ++u) {
672 if (ident == derived[u]) {
673 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
674 "Identity \"%s\" is indirectly derived from itself.", ident->name);
675 goto cleanup;
676 }
677 ly_set_add(&recursion, derived[u], 0);
678 }
679
680 for (v = 0; v < recursion.count; ++v) {
681 drv = recursion.objs[v];
682 if (!drv->derived) {
683 continue;
684 }
685 for (u = 0; u < LY_ARRAY_SIZE(drv->derived); ++u) {
686 if (ident == drv->derived[u]) {
687 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
688 "Identity \"%s\" is indirectly derived from itself.", ident->name);
689 goto cleanup;
690 }
691 ly_set_add(&recursion, drv->derived[u], 0);
692 }
693 }
694 ret = LY_SUCCESS;
695
696cleanup:
697 ly_set_erase(&recursion, NULL);
698 return ret;
699}
700
Radek Krejcia3045382018-11-22 14:30:31 +0100701/**
702 * @brief Find and process the referenced base identities from another identity or identityref
703 *
704 * For bases in identity se backlinks to them from the base identities. For identityref, store
705 * the array of pointers to the base identities. So one of the ident or bases parameter must be set
706 * to distinguish these two use cases.
707 *
708 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
709 * @param[in] bases_p Array of names (including prefix if necessary) of base identities.
710 * @param[in] ident Referencing identity to work with.
711 * @param[in] bases Array of bases of identityref to fill in.
712 * @return LY_ERR value.
713 */
Radek Krejci19a96102018-11-15 13:38:09 +0100714static LY_ERR
Radek Krejci555cb5b2018-11-16 14:54:33 +0100715lys_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 +0100716{
Radek Krejci555cb5b2018-11-16 14:54:33 +0100717 unsigned int u, v;
Radek Krejci19a96102018-11-15 13:38:09 +0100718 const char *s, *name;
Radek Krejcie86bf772018-12-14 11:39:53 +0100719 struct lys_module *mod;
Radek Krejci555cb5b2018-11-16 14:54:33 +0100720 struct lysc_ident **idref;
721
722 assert(ident || bases);
723
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100724 if (LY_ARRAY_SIZE(bases_p) > 1 && ctx->mod_def->version < 2) {
Radek Krejci555cb5b2018-11-16 14:54:33 +0100725 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
726 "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type");
727 return LY_EVALID;
728 }
729
730 for (u = 0; u < LY_ARRAY_SIZE(bases_p); ++u) {
731 s = strchr(bases_p[u], ':');
732 if (s) {
733 /* prefixed identity */
734 name = &s[1];
Radek Krejcie86bf772018-12-14 11:39:53 +0100735 mod = lys_module_find_prefix(ctx->mod_def, bases_p[u], s - bases_p[u]);
Radek Krejci555cb5b2018-11-16 14:54:33 +0100736 } else {
737 name = bases_p[u];
Radek Krejcie86bf772018-12-14 11:39:53 +0100738 mod = ctx->mod_def;
Radek Krejci555cb5b2018-11-16 14:54:33 +0100739 }
740 if (!mod) {
741 if (ident) {
742 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
743 "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name);
744 } else {
745 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
746 "Invalid prefix used for base (%s) of identityref.", bases_p[u]);
747 }
748 return LY_EVALID;
749 }
750 idref = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +0100751 if (mod->compiled && mod->compiled->identities) {
752 for (v = 0; v < LY_ARRAY_SIZE(mod->compiled->identities); ++v) {
753 if (!strcmp(name, mod->compiled->identities[v].name)) {
Radek Krejci555cb5b2018-11-16 14:54:33 +0100754 if (ident) {
Radek Krejci38222632019-02-12 16:55:05 +0100755 if (ident == &mod->compiled->identities[v]) {
756 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
757 "Identity \"%s\" is derived from itself.", ident->name);
758 return LY_EVALID;
759 }
760 LY_CHECK_RET(lys_compile_identity_circular_check(ctx, &mod->compiled->identities[v], ident->derived));
Radek Krejci555cb5b2018-11-16 14:54:33 +0100761 /* we have match! store the backlink */
Radek Krejcie86bf772018-12-14 11:39:53 +0100762 LY_ARRAY_NEW_RET(ctx->ctx, mod->compiled->identities[v].derived, idref, LY_EMEM);
Radek Krejci555cb5b2018-11-16 14:54:33 +0100763 *idref = ident;
764 } else {
765 /* we have match! store the found identity */
766 LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +0100767 *idref = &mod->compiled->identities[v];
Radek Krejci555cb5b2018-11-16 14:54:33 +0100768 }
769 break;
770 }
771 }
772 }
773 if (!idref || !(*idref)) {
774 if (ident) {
775 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
776 "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name);
777 } else {
778 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
779 "Unable to find base (%s) of identityref.", bases_p[u]);
780 }
781 return LY_EVALID;
782 }
783 }
784 return LY_SUCCESS;
785}
786
Radek Krejcia3045382018-11-22 14:30:31 +0100787/**
788 * @brief For the given array of identities, set the backlinks from all their base identities.
789 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
790 * @param[in] idents_p Array of identities definitions from the parsed schema structure.
791 * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set.
792 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
793 */
Radek Krejci555cb5b2018-11-16 14:54:33 +0100794static LY_ERR
795lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents)
796{
797 unsigned int i;
Radek Krejci19a96102018-11-15 13:38:09 +0100798
799 for (i = 0; i < LY_ARRAY_SIZE(idents_p); ++i) {
800 if (!idents_p[i].bases) {
801 continue;
802 }
Radek Krejci555cb5b2018-11-16 14:54:33 +0100803 LY_CHECK_RET(lys_compile_identity_bases(ctx, idents_p[i].bases, &idents[i], NULL));
Radek Krejci19a96102018-11-15 13:38:09 +0100804 }
805 return LY_SUCCESS;
806}
807
Radek Krejci0af46292019-01-11 16:02:31 +0100808LY_ERR
Radek Krejci693262f2019-04-29 15:23:20 +0200809lys_feature_precompile(struct ly_ctx *ctx, struct lys_module *module, struct lysp_feature *features_p, struct lysc_feature **features)
Radek Krejci0af46292019-01-11 16:02:31 +0100810{
811 unsigned int offset = 0, u;
812 struct lysc_ctx context = {0};
813
814 assert(ctx);
815 context.ctx = ctx;
816
817 if (!features_p) {
818 return LY_SUCCESS;
819 }
820 if (*features) {
821 offset = LY_ARRAY_SIZE(*features);
822 }
823
824 LY_ARRAY_CREATE_RET(ctx, *features, LY_ARRAY_SIZE(features_p), LY_EMEM);
825 LY_ARRAY_FOR(features_p, u) {
826 LY_ARRAY_INCREMENT(*features);
827 COMPILE_CHECK_UNIQUENESS(&context, *features, name, &(*features)[offset + u], "feature", features_p[u].name);
828 DUP_STRING(ctx, features_p[u].name, (*features)[offset + u].name);
829 DUP_STRING(ctx, features_p[u].dsc, (*features)[offset + u].dsc);
830 DUP_STRING(ctx, features_p[u].ref, (*features)[offset + u].ref);
831 (*features)[offset + u].flags = features_p[u].flags;
Radek Krejci693262f2019-04-29 15:23:20 +0200832 (*features)[offset + u].module = module;
Radek Krejci0af46292019-01-11 16:02:31 +0100833 }
834
835 return LY_SUCCESS;
836}
837
Radek Krejcia3045382018-11-22 14:30:31 +0100838/**
Radek Krejci09a1fc52019-02-13 10:55:17 +0100839 * @brief Check circular dependency of features - feature MUST NOT reference itself (via their if-feature statement).
Radek Krejcib56c7502019-02-13 14:19:54 +0100840 *
841 * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages.
842 *
Radek Krejci09a1fc52019-02-13 10:55:17 +0100843 * @param[in] ctx Compile context for logging.
Radek Krejcib56c7502019-02-13 14:19:54 +0100844 * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature
845 * being currently processed).
846 * @param[in] depfeatures The list of depending features of the feature being currently processed (not the one provided as @p feature)
Radek Krejci09a1fc52019-02-13 10:55:17 +0100847 * @return LY_SUCCESS if everything is ok.
848 * @return LY_EVALID if the feature references indirectly itself.
849 */
850static LY_ERR
851lys_compile_feature_circular_check(struct lysc_ctx *ctx, struct lysc_feature *feature, struct lysc_feature **depfeatures)
852{
853 LY_ERR ret = LY_EVALID;
854 unsigned int u, v;
855 struct ly_set recursion = {0};
856 struct lysc_feature *drv;
857
858 if (!depfeatures) {
859 return LY_SUCCESS;
860 }
861
862 for (u = 0; u < LY_ARRAY_SIZE(depfeatures); ++u) {
863 if (feature == depfeatures[u]) {
864 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
865 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
866 goto cleanup;
867 }
868 ly_set_add(&recursion, depfeatures[u], 0);
869 }
870
871 for (v = 0; v < recursion.count; ++v) {
872 drv = recursion.objs[v];
873 if (!drv->depfeatures) {
874 continue;
875 }
876 for (u = 0; u < LY_ARRAY_SIZE(drv->depfeatures); ++u) {
877 if (feature == drv->depfeatures[u]) {
878 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
879 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
880 goto cleanup;
881 }
882 ly_set_add(&recursion, drv->depfeatures[u], 0);
883 }
884 }
885 ret = LY_SUCCESS;
886
887cleanup:
888 ly_set_erase(&recursion, NULL);
889 return ret;
890}
891
892/**
Radek Krejci0af46292019-01-11 16:02:31 +0100893 * @brief Create pre-compiled features array.
894 *
895 * See lys_feature_precompile() for more details.
896 *
Radek Krejcia3045382018-11-22 14:30:31 +0100897 * @param[in] ctx Compile context.
898 * @param[in] feature_p Parsed feature definition to compile.
Radek Krejci0af46292019-01-11 16:02:31 +0100899 * @param[in,out] features List of already (pre)compiled features to find the corresponding precompiled feature structure.
Radek Krejcia3045382018-11-22 14:30:31 +0100900 * @return LY_ERR value.
901 */
Radek Krejci19a96102018-11-15 13:38:09 +0100902static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200903lys_feature_precompile_finish(struct lysc_ctx *ctx, struct lysp_feature *feature_p, struct lysc_feature *features)
Radek Krejci19a96102018-11-15 13:38:09 +0100904{
Radek Krejci0af46292019-01-11 16:02:31 +0100905 unsigned int u, v, x;
906 struct lysc_feature *feature, **df;
Radek Krejci19a96102018-11-15 13:38:09 +0100907 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +0100908
Radek Krejci0af46292019-01-11 16:02:31 +0100909 /* find the preprecompiled feature */
910 LY_ARRAY_FOR(features, x) {
911 if (strcmp(features[x].name, feature_p->name)) {
912 continue;
913 }
914 feature = &features[x];
Radek Krejci19a96102018-11-15 13:38:09 +0100915
Radek Krejci0af46292019-01-11 16:02:31 +0100916 /* finish compilation started in lys_feature_precompile() */
Radek Krejciec4da802019-05-02 13:02:41 +0200917 COMPILE_ARRAY_GOTO(ctx, feature_p->exts, feature->exts, u, lys_compile_ext, ret, done);
918 COMPILE_ARRAY_GOTO(ctx, feature_p->iffeatures, feature->iffeatures, u, lys_compile_iffeature, ret, done);
Radek Krejci0af46292019-01-11 16:02:31 +0100919 if (feature->iffeatures) {
920 for (u = 0; u < LY_ARRAY_SIZE(feature->iffeatures); ++u) {
921 if (feature->iffeatures[u].features) {
922 for (v = 0; v < LY_ARRAY_SIZE(feature->iffeatures[u].features); ++v) {
Radek Krejci09a1fc52019-02-13 10:55:17 +0100923 /* check for circular dependency - direct reference first,... */
924 if (feature == feature->iffeatures[u].features[v]) {
925 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
926 "Feature \"%s\" is referenced from itself.", feature->name);
927 return LY_EVALID;
928 }
929 /* ... and indirect circular reference */
930 LY_CHECK_RET(lys_compile_feature_circular_check(ctx, feature->iffeatures[u].features[v], feature->depfeatures));
931
Radek Krejci0af46292019-01-11 16:02:31 +0100932 /* add itself into the dependants list */
933 LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM);
934 *df = feature;
935 }
Radek Krejci19a96102018-11-15 13:38:09 +0100936 }
Radek Krejci19a96102018-11-15 13:38:09 +0100937 }
938 }
Radek Krejci0af46292019-01-11 16:02:31 +0100939 done:
940 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +0100941 }
Radek Krejci0af46292019-01-11 16:02:31 +0100942
943 LOGINT(ctx->ctx);
944 return LY_EINT;
Radek Krejci19a96102018-11-15 13:38:09 +0100945}
946
Radek Krejcib56c7502019-02-13 14:19:54 +0100947/**
948 * @brief Revert compiled list of features back to the precompiled state.
949 *
950 * Function is needed in case the compilation failed and the schema is expected to revert back to the non-compiled status.
951 * The features are supposed to be stored again as off_features in ::lys_module structure.
952 *
953 * @param[in] ctx Compilation context.
954 * @param[in] mod The module structure still holding the compiled (but possibly not finished, only the list of compiled features is taken) schema
955 * and supposed to hold the off_features list.
956 */
Radek Krejci95710c92019-02-11 15:49:55 +0100957static void
958lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod)
959{
960 unsigned int u, v;
961
962 /* keep the off_features list until the complete lys_module is freed */
963 mod->off_features = mod->compiled->features;
964 mod->compiled->features = NULL;
965
966 /* in the off_features list, remove all the parts (from finished compiling process)
967 * which may points into the data being freed here */
968 LY_ARRAY_FOR(mod->off_features, u) {
969 LY_ARRAY_FOR(mod->off_features[u].iffeatures, v) {
970 lysc_iffeature_free(ctx->ctx, &mod->off_features[u].iffeatures[v]);
971 }
972 LY_ARRAY_FREE(mod->off_features[u].iffeatures);
973 mod->off_features[u].iffeatures = NULL;
974
975 LY_ARRAY_FOR(mod->off_features[u].exts, v) {
976 lysc_ext_instance_free(ctx->ctx, &(mod->off_features[u].exts)[v]);
977 }
978 LY_ARRAY_FREE(mod->off_features[u].exts);
979 mod->off_features[u].exts = NULL;
980 }
981}
982
Radek Krejcia3045382018-11-22 14:30:31 +0100983/**
984 * @brief Validate and normalize numeric value from a range definition.
985 * @param[in] ctx Compile context.
986 * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to
987 * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into
988 * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from
989 * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100.
990 * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64.
991 * @param[in] value String value of the range boundary.
992 * @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.
993 * @param[out] valcopy NULL-terminated string with the numeric value to parse and store.
994 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value).
995 */
Radek Krejci19a96102018-11-15 13:38:09 +0100996static LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +0100997range_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 +0100998{
Radek Krejci6cba4292018-11-15 17:33:29 +0100999 size_t fraction = 0, size;
1000
Radek Krejci19a96102018-11-15 13:38:09 +01001001 *len = 0;
1002
1003 assert(value);
1004 /* parse value */
1005 if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) {
1006 return LY_EVALID;
1007 }
1008
1009 if ((value[*len] == '-') || (value[*len] == '+')) {
1010 ++(*len);
1011 }
1012
1013 while (isdigit(value[*len])) {
1014 ++(*len);
1015 }
1016
1017 if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001018 if (basetype == LY_TYPE_DEC64) {
1019 goto decimal;
1020 } else {
1021 *valcopy = strndup(value, *len);
1022 return LY_SUCCESS;
1023 }
Radek Krejci19a96102018-11-15 13:38:09 +01001024 }
1025 fraction = *len;
1026
1027 ++(*len);
1028 while (isdigit(value[*len])) {
1029 ++(*len);
1030 }
1031
Radek Krejci6cba4292018-11-15 17:33:29 +01001032 if (basetype == LY_TYPE_DEC64) {
1033decimal:
1034 assert(frdigits);
1035 if (*len - 1 - fraction > frdigits) {
1036 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1037 "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.",
1038 *len, value, frdigits);
1039 return LY_EINVAL;
1040 }
1041 if (fraction) {
1042 size = (*len) + (frdigits - ((*len) - 1 - fraction));
1043 } else {
1044 size = (*len) + frdigits + 1;
1045 }
1046 *valcopy = malloc(size * sizeof **valcopy);
Radek Krejci19a96102018-11-15 13:38:09 +01001047 LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM);
1048
Radek Krejci6cba4292018-11-15 17:33:29 +01001049 (*valcopy)[size - 1] = '\0';
1050 if (fraction) {
1051 memcpy(&(*valcopy)[0], &value[0], fraction);
1052 memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction));
1053 memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction));
1054 } else {
1055 memcpy(&(*valcopy)[0], &value[0], *len);
1056 memset(&(*valcopy)[*len], '0', frdigits);
1057 }
Radek Krejci19a96102018-11-15 13:38:09 +01001058 }
1059 return LY_SUCCESS;
1060}
1061
Radek Krejcia3045382018-11-22 14:30:31 +01001062/**
1063 * @brief Check that values in range are in ascendant order.
1064 * @param[in] unsigned_value Flag to note that we are working with unsigned values.
Radek Krejci5969f272018-11-23 10:03:58 +01001065 * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous,
1066 * max can be also equal.
Radek Krejcia3045382018-11-22 14:30:31 +01001067 * @param[in] value Current value to check.
1068 * @param[in] prev_value The last seen value.
1069 * @return LY_SUCCESS or LY_EEXIST for invalid order.
1070 */
Radek Krejci19a96102018-11-15 13:38:09 +01001071static LY_ERR
Radek Krejci5969f272018-11-23 10:03:58 +01001072range_part_check_ascendancy(int unsigned_value, int max, int64_t value, int64_t prev_value)
Radek Krejci19a96102018-11-15 13:38:09 +01001073{
1074 if (unsigned_value) {
Radek Krejci5969f272018-11-23 10:03:58 +01001075 if ((max && (uint64_t)prev_value > (uint64_t)value) || (!max && (uint64_t)prev_value >= (uint64_t)value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001076 return LY_EEXIST;
1077 }
1078 } else {
Radek Krejci5969f272018-11-23 10:03:58 +01001079 if ((max && prev_value > value) || (!max && prev_value >= value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001080 return LY_EEXIST;
1081 }
1082 }
1083 return LY_SUCCESS;
1084}
1085
Radek Krejcia3045382018-11-22 14:30:31 +01001086/**
1087 * @brief Set min/max value of the range part.
1088 * @param[in] ctx Compile context.
1089 * @param[in] part Range part structure to fill.
1090 * @param[in] max Flag to distinguish if storing min or max value.
1091 * @param[in] prev The last seen value to check that all values in range are specified in ascendant order.
1092 * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules.
1093 * @param[in] first Flag for the first value of the range to avoid ascendancy order.
1094 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1095 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
Radek Krejci5969f272018-11-23 10:03:58 +01001096 * @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 +01001097 * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set.
1098 * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number),
1099 * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the
1100 * frdigits value), LY_EMEM.
1101 */
Radek Krejci19a96102018-11-15 13:38:09 +01001102static LY_ERR
1103range_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 +01001104 uint8_t frdigits, struct lysc_range *base_range, const char **value)
Radek Krejci19a96102018-11-15 13:38:09 +01001105{
1106 LY_ERR ret = LY_SUCCESS;
1107 char *valcopy = NULL;
1108 size_t len;
1109
1110 if (value) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001111 ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy);
Radek Krejci5969f272018-11-23 10:03:58 +01001112 LY_CHECK_GOTO(ret, finalize);
1113 }
1114 if (!valcopy && base_range) {
1115 if (max) {
1116 part->max_64 = base_range->parts[LY_ARRAY_SIZE(base_range->parts) - 1].max_64;
1117 } else {
1118 part->min_64 = base_range->parts[0].min_64;
1119 }
1120 if (!first) {
1121 ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev);
1122 }
1123 goto finalize;
Radek Krejci19a96102018-11-15 13:38:09 +01001124 }
1125
1126 switch (basetype) {
Radek Krejci19a96102018-11-15 13:38:09 +01001127 case LY_TYPE_INT8: /* range */
1128 if (valcopy) {
1129 ret = ly_parse_int(valcopy, INT64_C(-128), INT64_C(127), 10, max ? &part->max_64 : &part->min_64);
1130 } else if (max) {
1131 part->max_64 = INT64_C(127);
1132 } else {
1133 part->min_64 = INT64_C(-128);
1134 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001135 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001136 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001137 }
1138 break;
1139 case LY_TYPE_INT16: /* range */
1140 if (valcopy) {
1141 ret = ly_parse_int(valcopy, INT64_C(-32768), INT64_C(32767), 10, max ? &part->max_64 : &part->min_64);
1142 } else if (max) {
1143 part->max_64 = INT64_C(32767);
1144 } else {
1145 part->min_64 = INT64_C(-32768);
1146 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001147 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001148 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001149 }
1150 break;
1151 case LY_TYPE_INT32: /* range */
1152 if (valcopy) {
1153 ret = ly_parse_int(valcopy, INT64_C(-2147483648), INT64_C(2147483647), 10, max ? &part->max_64 : &part->min_64);
1154 } else if (max) {
1155 part->max_64 = INT64_C(2147483647);
1156 } else {
1157 part->min_64 = INT64_C(-2147483648);
1158 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001159 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001160 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001161 }
1162 break;
1163 case LY_TYPE_INT64: /* range */
Radek Krejci25cfef72018-11-23 14:15:52 +01001164 case LY_TYPE_DEC64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001165 if (valcopy) {
1166 ret = ly_parse_int(valcopy, INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807), 10,
1167 max ? &part->max_64 : &part->min_64);
1168 } else if (max) {
1169 part->max_64 = INT64_C(9223372036854775807);
1170 } else {
1171 part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1);
1172 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001173 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001174 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001175 }
1176 break;
1177 case LY_TYPE_UINT8: /* range */
1178 if (valcopy) {
1179 ret = ly_parse_uint(valcopy, UINT64_C(255), 10, max ? &part->max_u64 : &part->min_u64);
1180 } else if (max) {
1181 part->max_u64 = UINT64_C(255);
1182 } else {
1183 part->min_u64 = UINT64_C(0);
1184 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001185 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001186 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001187 }
1188 break;
1189 case LY_TYPE_UINT16: /* range */
1190 if (valcopy) {
1191 ret = ly_parse_uint(valcopy, UINT64_C(65535), 10, max ? &part->max_u64 : &part->min_u64);
1192 } else if (max) {
1193 part->max_u64 = UINT64_C(65535);
1194 } else {
1195 part->min_u64 = UINT64_C(0);
1196 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001197 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001198 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001199 }
1200 break;
1201 case LY_TYPE_UINT32: /* range */
1202 if (valcopy) {
1203 ret = ly_parse_uint(valcopy, UINT64_C(4294967295), 10, max ? &part->max_u64 : &part->min_u64);
1204 } else if (max) {
1205 part->max_u64 = UINT64_C(4294967295);
1206 } else {
1207 part->min_u64 = UINT64_C(0);
1208 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001209 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001210 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001211 }
1212 break;
1213 case LY_TYPE_UINT64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001214 case LY_TYPE_STRING: /* length */
Radek Krejci25cfef72018-11-23 14:15:52 +01001215 case LY_TYPE_BINARY: /* length */
Radek Krejci19a96102018-11-15 13:38:09 +01001216 if (valcopy) {
1217 ret = ly_parse_uint(valcopy, UINT64_C(18446744073709551615), 10, max ? &part->max_u64 : &part->min_u64);
1218 } else if (max) {
1219 part->max_u64 = UINT64_C(18446744073709551615);
1220 } else {
1221 part->min_u64 = UINT64_C(0);
1222 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001223 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001224 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001225 }
1226 break;
1227 default:
1228 LOGINT(ctx->ctx);
1229 ret = LY_EINT;
1230 }
1231
Radek Krejci5969f272018-11-23 10:03:58 +01001232finalize:
Radek Krejci19a96102018-11-15 13:38:09 +01001233 if (ret == LY_EDENIED) {
1234 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1235 "Invalid %s restriction - value \"%s\" does not fit the type limitations.",
1236 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1237 } else if (ret == LY_EVALID) {
1238 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1239 "Invalid %s restriction - invalid value \"%s\".",
1240 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1241 } else if (ret == LY_EEXIST) {
1242 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1243 "Invalid %s restriction - values are not in ascending order (%s).",
Radek Krejci6cba4292018-11-15 17:33:29 +01001244 length_restr ? "length" : "range",
Radek Krejci5969f272018-11-23 10:03:58 +01001245 (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min");
Radek Krejci19a96102018-11-15 13:38:09 +01001246 } else if (!ret && value) {
1247 *value = *value + len;
1248 }
1249 free(valcopy);
1250 return ret;
1251}
1252
Radek Krejcia3045382018-11-22 14:30:31 +01001253/**
1254 * @brief Compile the parsed range restriction.
1255 * @param[in] ctx Compile context.
1256 * @param[in] range_p Parsed range structure to compile.
1257 * @param[in] basetype Base YANG built-in type of the node with the range restriction.
1258 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1259 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
1260 * @param[in] base_range Range restriction of the type from which the current type is derived. The current
1261 * range restriction must be more restrictive than the base_range.
1262 * @param[in,out] range Pointer to the created current range structure.
1263 * @return LY_ERR value.
1264 */
Radek Krejci19a96102018-11-15 13:38:09 +01001265static LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001266lys_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 +01001267 struct lysc_range *base_range, struct lysc_range **range)
1268{
1269 LY_ERR ret = LY_EVALID;
1270 const char *expr;
1271 struct lysc_range_part *parts = NULL, *part;
1272 int range_expected = 0, uns;
1273 unsigned int parts_done = 0, u, v;
1274
1275 assert(range);
1276 assert(range_p);
1277
1278 expr = range_p->arg;
1279 while(1) {
1280 if (isspace(*expr)) {
1281 ++expr;
1282 } else if (*expr == '\0') {
1283 if (range_expected) {
1284 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1285 "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).",
1286 length_restr ? "length" : "range", range_p->arg);
1287 goto cleanup;
1288 } else if (!parts || parts_done == LY_ARRAY_SIZE(parts)) {
1289 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1290 "Invalid %s restriction - unexpected end of the expression (%s).",
1291 length_restr ? "length" : "range", range_p->arg);
1292 goto cleanup;
1293 }
1294 parts_done++;
1295 break;
1296 } else if (!strncmp(expr, "min", 3)) {
1297 if (parts) {
1298 /* min cannot be used elsewhere than in the first part */
1299 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1300 "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range",
1301 expr - range_p->arg, range_p->arg);
1302 goto cleanup;
1303 }
1304 expr += 3;
1305
1306 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Radek Krejci5969f272018-11-23 10:03:58 +01001307 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 +01001308 part->max_64 = part->min_64;
1309 } else if (*expr == '|') {
1310 if (!parts || range_expected) {
1311 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1312 "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr);
1313 goto cleanup;
1314 }
1315 expr++;
1316 parts_done++;
1317 /* process next part of the expression */
1318 } else if (!strncmp(expr, "..", 2)) {
1319 expr += 2;
1320 while (isspace(*expr)) {
1321 expr++;
1322 }
1323 if (!parts || LY_ARRAY_SIZE(parts) == parts_done) {
1324 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1325 "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range");
1326 goto cleanup;
1327 }
1328 /* continue expecting the upper boundary */
1329 range_expected = 1;
1330 } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) {
1331 /* number */
1332 if (range_expected) {
1333 part = &parts[LY_ARRAY_SIZE(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001334 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 +01001335 range_expected = 0;
1336 } else {
1337 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
1338 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 +01001339 basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001340 part->max_64 = part->min_64;
1341 }
1342
1343 /* continue with possible another expression part */
1344 } else if (!strncmp(expr, "max", 3)) {
1345 expr += 3;
1346 while (isspace(*expr)) {
1347 expr++;
1348 }
1349 if (*expr != '\0') {
1350 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).",
1351 length_restr ? "length" : "range", expr);
1352 goto cleanup;
1353 }
1354 if (range_expected) {
1355 part = &parts[LY_ARRAY_SIZE(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001356 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 +01001357 range_expected = 0;
1358 } else {
1359 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
1360 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 +01001361 basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001362 part->min_64 = part->max_64;
1363 }
1364 } else {
1365 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).",
1366 length_restr ? "length" : "range", expr);
1367 goto cleanup;
1368 }
1369 }
1370
1371 /* check with the previous range/length restriction */
1372 if (base_range) {
1373 switch (basetype) {
1374 case LY_TYPE_BINARY:
1375 case LY_TYPE_UINT8:
1376 case LY_TYPE_UINT16:
1377 case LY_TYPE_UINT32:
1378 case LY_TYPE_UINT64:
1379 case LY_TYPE_STRING:
1380 uns = 1;
1381 break;
1382 case LY_TYPE_DEC64:
1383 case LY_TYPE_INT8:
1384 case LY_TYPE_INT16:
1385 case LY_TYPE_INT32:
1386 case LY_TYPE_INT64:
1387 uns = 0;
1388 break;
1389 default:
1390 LOGINT(ctx->ctx);
1391 ret = LY_EINT;
1392 goto cleanup;
1393 }
1394 for (u = v = 0; u < parts_done && v < LY_ARRAY_SIZE(base_range->parts); ++u) {
1395 if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) {
1396 goto baseerror;
1397 }
1398 /* current lower bound is not lower than the base */
1399 if (base_range->parts[v].min_64 == base_range->parts[v].max_64) {
1400 /* base has single value */
1401 if (base_range->parts[v].min_64 == parts[u].min_64) {
1402 /* both lower bounds are the same */
1403 if (parts[u].min_64 != parts[u].max_64) {
1404 /* current continues with a range */
1405 goto baseerror;
1406 } else {
1407 /* equal single values, move both forward */
1408 ++v;
1409 continue;
1410 }
1411 } else {
1412 /* base is single value lower than current range, so the
1413 * value from base range is removed in the current,
1414 * move only base and repeat checking */
1415 ++v;
1416 --u;
1417 continue;
1418 }
1419 } else {
1420 /* base is the range */
1421 if (parts[u].min_64 == parts[u].max_64) {
1422 /* current is a single value */
1423 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1424 /* current is behind the base range, so base range is omitted,
1425 * move the base and keep the current for further check */
1426 ++v;
1427 --u;
1428 } /* else it is within the base range, so move the current, but keep the base */
1429 continue;
1430 } else {
1431 /* both are ranges - check the higher bound, the lower was already checked */
1432 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1433 /* higher bound is higher than the current higher bound */
1434 if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) {
1435 /* but the current lower bound is also higher, so the base range is omitted,
1436 * continue with the same current, but move the base */
1437 --u;
1438 ++v;
1439 continue;
1440 }
1441 /* current range starts within the base range but end behind it */
1442 goto baseerror;
1443 } else {
1444 /* current range is smaller than the base,
1445 * move current, but stay with the base */
1446 continue;
1447 }
1448 }
1449 }
1450 }
1451 if (u != parts_done) {
1452baseerror:
1453 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1454 "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.",
1455 length_restr ? "length" : "range", range_p->arg);
1456 goto cleanup;
1457 }
1458 }
1459
1460 if (!(*range)) {
1461 *range = calloc(1, sizeof **range);
1462 LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM);
1463 }
1464
Radek Krejcic8b31002019-01-08 10:24:45 +01001465 /* we rewrite the following values as the types chain is being processed */
Radek Krejci19a96102018-11-15 13:38:09 +01001466 if (range_p->eapptag) {
1467 lydict_remove(ctx->ctx, (*range)->eapptag);
1468 (*range)->eapptag = lydict_insert(ctx->ctx, range_p->eapptag, 0);
1469 }
1470 if (range_p->emsg) {
1471 lydict_remove(ctx->ctx, (*range)->emsg);
1472 (*range)->emsg = lydict_insert(ctx->ctx, range_p->emsg, 0);
1473 }
Radek Krejcic8b31002019-01-08 10:24:45 +01001474 if (range_p->dsc) {
1475 lydict_remove(ctx->ctx, (*range)->dsc);
1476 (*range)->dsc = lydict_insert(ctx->ctx, range_p->dsc, 0);
1477 }
1478 if (range_p->ref) {
1479 lydict_remove(ctx->ctx, (*range)->ref);
1480 (*range)->ref = lydict_insert(ctx->ctx, range_p->ref, 0);
1481 }
Radek Krejci19a96102018-11-15 13:38:09 +01001482 /* extensions are taken only from the last range by the caller */
1483
1484 (*range)->parts = parts;
1485 parts = NULL;
1486 ret = LY_SUCCESS;
1487cleanup:
Radek Krejci19a96102018-11-15 13:38:09 +01001488 LY_ARRAY_FREE(parts);
1489
1490 return ret;
1491}
1492
1493/**
1494 * @brief Checks pattern syntax.
1495 *
Radek Krejcia3045382018-11-22 14:30:31 +01001496 * @param[in] ctx Compile context.
Radek Krejci19a96102018-11-15 13:38:09 +01001497 * @param[in] pattern Pattern to check.
Radek Krejci54579462019-04-30 12:47:06 +02001498 * @param[in,out] pcre2_code Compiled PCRE2 pattern. If NULL, the compiled information used to validate pattern are freed.
Radek Krejcia3045382018-11-22 14:30:31 +01001499 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID.
Radek Krejci19a96102018-11-15 13:38:09 +01001500 */
1501static LY_ERR
Radek Krejci54579462019-04-30 12:47:06 +02001502lys_compile_type_pattern_check(struct lysc_ctx *ctx, const char *pattern, pcre2_code **code)
Radek Krejci19a96102018-11-15 13:38:09 +01001503{
Radek Krejci54579462019-04-30 12:47:06 +02001504 int idx, idx2, start, end, count;
Radek Krejci19a96102018-11-15 13:38:09 +01001505 char *perl_regex, *ptr;
Radek Krejci54579462019-04-30 12:47:06 +02001506 int err_code;
1507 const char *orig_ptr;
1508 PCRE2_SIZE err_offset;
1509 pcre2_code *code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01001510#define URANGE_LEN 19
1511 char *ublock2urange[][2] = {
1512 {"BasicLatin", "[\\x{0000}-\\x{007F}]"},
1513 {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"},
1514 {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"},
1515 {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"},
1516 {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"},
1517 {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"},
1518 {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"},
1519 {"Greek", "[\\x{0370}-\\x{03FF}]"},
1520 {"Cyrillic", "[\\x{0400}-\\x{04FF}]"},
1521 {"Armenian", "[\\x{0530}-\\x{058F}]"},
1522 {"Hebrew", "[\\x{0590}-\\x{05FF}]"},
1523 {"Arabic", "[\\x{0600}-\\x{06FF}]"},
1524 {"Syriac", "[\\x{0700}-\\x{074F}]"},
1525 {"Thaana", "[\\x{0780}-\\x{07BF}]"},
1526 {"Devanagari", "[\\x{0900}-\\x{097F}]"},
1527 {"Bengali", "[\\x{0980}-\\x{09FF}]"},
1528 {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"},
1529 {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"},
1530 {"Oriya", "[\\x{0B00}-\\x{0B7F}]"},
1531 {"Tamil", "[\\x{0B80}-\\x{0BFF}]"},
1532 {"Telugu", "[\\x{0C00}-\\x{0C7F}]"},
1533 {"Kannada", "[\\x{0C80}-\\x{0CFF}]"},
1534 {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"},
1535 {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"},
1536 {"Thai", "[\\x{0E00}-\\x{0E7F}]"},
1537 {"Lao", "[\\x{0E80}-\\x{0EFF}]"},
1538 {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"},
1539 {"Myanmar", "[\\x{1000}-\\x{109F}]"},
1540 {"Georgian", "[\\x{10A0}-\\x{10FF}]"},
1541 {"HangulJamo", "[\\x{1100}-\\x{11FF}]"},
1542 {"Ethiopic", "[\\x{1200}-\\x{137F}]"},
1543 {"Cherokee", "[\\x{13A0}-\\x{13FF}]"},
1544 {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"},
1545 {"Ogham", "[\\x{1680}-\\x{169F}]"},
1546 {"Runic", "[\\x{16A0}-\\x{16FF}]"},
1547 {"Khmer", "[\\x{1780}-\\x{17FF}]"},
1548 {"Mongolian", "[\\x{1800}-\\x{18AF}]"},
1549 {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"},
1550 {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"},
1551 {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"},
1552 {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"},
1553 {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"},
1554 {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"},
1555 {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"},
1556 {"NumberForms", "[\\x{2150}-\\x{218F}]"},
1557 {"Arrows", "[\\x{2190}-\\x{21FF}]"},
1558 {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"},
1559 {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"},
1560 {"ControlPictures", "[\\x{2400}-\\x{243F}]"},
1561 {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"},
1562 {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"},
1563 {"BoxDrawing", "[\\x{2500}-\\x{257F}]"},
1564 {"BlockElements", "[\\x{2580}-\\x{259F}]"},
1565 {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"},
1566 {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"},
1567 {"Dingbats", "[\\x{2700}-\\x{27BF}]"},
1568 {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"},
1569 {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"},
1570 {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"},
1571 {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"},
1572 {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"},
1573 {"Hiragana", "[\\x{3040}-\\x{309F}]"},
1574 {"Katakana", "[\\x{30A0}-\\x{30FF}]"},
1575 {"Bopomofo", "[\\x{3100}-\\x{312F}]"},
1576 {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"},
1577 {"Kanbun", "[\\x{3190}-\\x{319F}]"},
1578 {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"},
1579 {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"},
1580 {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"},
1581 {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"},
1582 {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"},
1583 {"YiSyllables", "[\\x{A000}-\\x{A48F}]"},
1584 {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"},
1585 {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"},
1586 {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"},
1587 {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"},
1588 {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"},
1589 {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"},
1590 {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"},
1591 {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"},
1592 {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"},
1593 {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"},
1594 {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"},
1595 {NULL, NULL}
1596 };
1597
1598 /* adjust the expression to a Perl equivalent
1599 * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */
1600
1601 /* we need to replace all "$" with "\$", count them now */
1602 for (count = 0, ptr = strpbrk(pattern, "^$"); ptr; ++count, ptr = strpbrk(ptr + 1, "^$"));
1603
1604 perl_regex = malloc((strlen(pattern) + 4 + count) * sizeof(char));
1605 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx), LY_EMEM);
1606 perl_regex[0] = '\0';
1607
1608 ptr = perl_regex;
1609
1610 if (strncmp(pattern + strlen(pattern) - 2, ".*", 2)) {
1611 /* we will add line-end anchoring */
1612 ptr[0] = '(';
1613 ++ptr;
1614 }
1615
1616 for (orig_ptr = pattern; orig_ptr[0]; ++orig_ptr) {
1617 if (orig_ptr[0] == '$') {
1618 ptr += sprintf(ptr, "\\$");
1619 } else if (orig_ptr[0] == '^') {
1620 ptr += sprintf(ptr, "\\^");
1621 } else {
1622 ptr[0] = orig_ptr[0];
1623 ++ptr;
1624 }
1625 }
1626
1627 if (strncmp(pattern + strlen(pattern) - 2, ".*", 2)) {
1628 ptr += sprintf(ptr, ")$");
1629 } else {
1630 ptr[0] = '\0';
1631 ++ptr;
1632 }
1633
1634 /* substitute Unicode Character Blocks with exact Character Ranges */
1635 while ((ptr = strstr(perl_regex, "\\p{Is"))) {
1636 start = ptr - perl_regex;
1637
1638 ptr = strchr(ptr, '}');
1639 if (!ptr) {
1640 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP,
1641 pattern, perl_regex + start + 2, "unterminated character property");
1642 free(perl_regex);
1643 return LY_EVALID;
1644 }
1645 end = (ptr - perl_regex) + 1;
1646
1647 /* need more space */
1648 if (end - start < URANGE_LEN) {
1649 perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1);
1650 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx); free(perl_regex), LY_EMEM);
1651 }
1652
1653 /* find our range */
1654 for (idx = 0; ublock2urange[idx][0]; ++idx) {
1655 if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) {
1656 break;
1657 }
1658 }
1659 if (!ublock2urange[idx][0]) {
1660 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP,
1661 pattern, perl_regex + start + 5, "unknown block name");
1662 free(perl_regex);
1663 return LY_EVALID;
1664 }
1665
1666 /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */
1667 for (idx2 = 0, count = 0; idx2 < start; ++idx2) {
1668 if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1669 ++count;
1670 }
1671 if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1672 --count;
1673 }
1674 }
1675 if (count) {
1676 /* skip brackets */
1677 memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1);
1678 memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2);
1679 } else {
1680 memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1);
1681 memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN);
1682 }
1683 }
1684
1685 /* must return 0, already checked during parsing */
Radek Krejci54579462019-04-30 12:47:06 +02001686 code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED, PCRE2_UTF | PCRE2_ANCHORED | PCRE2_DOLLAR_ENDONLY | PCRE2_NO_AUTO_CAPTURE,
1687 &err_code, &err_offset, NULL);
1688 if (!code_local) {
1689 PCRE2_UCHAR err_msg[256] = {0};
1690 pcre2_get_error_message(err_code, err_msg, 256);
Radek Krejci19a96102018-11-15 13:38:09 +01001691 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
1692 free(perl_regex);
1693 return LY_EVALID;
1694 }
1695 free(perl_regex);
1696
Radek Krejci54579462019-04-30 12:47:06 +02001697 if (code) {
1698 *code = code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01001699 } else {
Radek Krejci54579462019-04-30 12:47:06 +02001700 free(code_local);
Radek Krejci19a96102018-11-15 13:38:09 +01001701 }
1702
1703 return LY_SUCCESS;
1704
1705#undef URANGE_LEN
1706}
1707
Radek Krejcia3045382018-11-22 14:30:31 +01001708/**
1709 * @brief Compile parsed pattern restriction in conjunction with the patterns from base type.
1710 * @param[in] ctx Compile context.
1711 * @param[in] patterns_p Array of parsed patterns from the current type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01001712 * @param[in] base_patterns Compiled patterns from the type from which the current type is derived.
1713 * Patterns from the base type are inherited to have all the patterns that have to match at one place.
1714 * @param[out] patterns Pointer to the storage for the patterns of the current type.
1715 * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID.
1716 */
Radek Krejci19a96102018-11-15 13:38:09 +01001717static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02001718lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p,
Radek Krejci19a96102018-11-15 13:38:09 +01001719 struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns)
1720{
1721 struct lysc_pattern **pattern;
1722 unsigned int u, v;
Radek Krejci19a96102018-11-15 13:38:09 +01001723 LY_ERR ret = LY_SUCCESS;
1724
1725 /* first, copy the patterns from the base type */
1726 if (base_patterns) {
1727 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
1728 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
1729 }
1730
1731 LY_ARRAY_FOR(patterns_p, u) {
1732 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
1733 *pattern = calloc(1, sizeof **pattern);
1734 ++(*pattern)->refcount;
1735
Radek Krejci54579462019-04-30 12:47:06 +02001736 ret = lys_compile_type_pattern_check(ctx, &patterns_p[u].arg[1], &(*pattern)->code);
Radek Krejci19a96102018-11-15 13:38:09 +01001737 LY_CHECK_RET(ret);
Radek Krejci19a96102018-11-15 13:38:09 +01001738
1739 if (patterns_p[u].arg[0] == 0x15) {
1740 (*pattern)->inverted = 1;
1741 }
Radek Krejci54579462019-04-30 12:47:06 +02001742 DUP_STRING(ctx->ctx, &patterns_p[u].arg[1], (*pattern)->expr);
Radek Krejci19a96102018-11-15 13:38:09 +01001743 DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag);
1744 DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +01001745 DUP_STRING(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc);
1746 DUP_STRING(ctx->ctx, patterns_p[u].ref, (*pattern)->ref);
Radek Krejciec4da802019-05-02 13:02:41 +02001747 COMPILE_ARRAY_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts, v, lys_compile_ext, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01001748 }
1749done:
1750 return ret;
1751}
1752
Radek Krejcia3045382018-11-22 14:30:31 +01001753/**
1754 * @brief map of the possible restrictions combination for the specific built-in type.
1755 */
Radek Krejci19a96102018-11-15 13:38:09 +01001756static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
1757 0 /* LY_TYPE_UNKNOWN */,
1758 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
Radek Krejci5969f272018-11-23 10:03:58 +01001759 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
1760 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
1761 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
1762 LYS_SET_RANGE /* LY_TYPE_UINT64 */,
1763 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
Radek Krejci19a96102018-11-15 13:38:09 +01001764 LYS_SET_BIT /* LY_TYPE_BITS */,
1765 0 /* LY_TYPE_BOOL */,
1766 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
1767 0 /* LY_TYPE_EMPTY */,
1768 LYS_SET_ENUM /* LY_TYPE_ENUM */,
1769 LYS_SET_BASE /* LY_TYPE_IDENT */,
1770 LYS_SET_REQINST /* LY_TYPE_INST */,
1771 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
Radek Krejci19a96102018-11-15 13:38:09 +01001772 LYS_SET_TYPE /* LY_TYPE_UNION */,
1773 LYS_SET_RANGE /* LY_TYPE_INT8 */,
Radek Krejci19a96102018-11-15 13:38:09 +01001774 LYS_SET_RANGE /* LY_TYPE_INT16 */,
Radek Krejci19a96102018-11-15 13:38:09 +01001775 LYS_SET_RANGE /* LY_TYPE_INT32 */,
Radek Krejci5969f272018-11-23 10:03:58 +01001776 LYS_SET_RANGE /* LY_TYPE_INT64 */
1777};
1778
1779/**
1780 * @brief stringification of the YANG built-in data types
1781 */
1782const char* ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer",
1783 "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration",
1784 "identityref", "instance-identifier", "leafref", "union", "8bit integer", "16bit integer", "32bit integer", "64bit integer"
Radek Krejci19a96102018-11-15 13:38:09 +01001785};
1786
Radek Krejcia3045382018-11-22 14:30:31 +01001787/**
1788 * @brief Compile parsed type's enum structures (for enumeration and bits types).
1789 * @param[in] ctx Compile context.
1790 * @param[in] enums_p Array of the parsed enum structures to compile.
1791 * @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.
Radek Krejcia3045382018-11-22 14:30:31 +01001792 * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible.
1793 * @param[out] enums Newly created array of the compiled enums information for the current type.
1794 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1795 */
Radek Krejci19a96102018-11-15 13:38:09 +01001796static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02001797lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype,
Radek Krejci693262f2019-04-29 15:23:20 +02001798 struct lysc_type_bitenum_item *base_enums, struct lysc_type_bitenum_item **enums)
Radek Krejci19a96102018-11-15 13:38:09 +01001799{
1800 LY_ERR ret = LY_SUCCESS;
1801 unsigned int u, v, match;
1802 int32_t value = 0;
1803 uint32_t position = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02001804 struct lysc_type_bitenum_item *e, storage;
Radek Krejci19a96102018-11-15 13:38:09 +01001805
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001806 if (base_enums && ctx->mod_def->version < 2) {
Radek Krejci19a96102018-11-15 13:38:09 +01001807 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
1808 basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
1809 return LY_EVALID;
1810 }
1811
1812 LY_ARRAY_FOR(enums_p, u) {
1813 LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM);
1814 DUP_STRING(ctx->ctx, enums_p[u].name, e->name);
Radek Krejcic8b31002019-01-08 10:24:45 +01001815 DUP_STRING(ctx->ctx, enums_p[u].ref, e->dsc);
1816 DUP_STRING(ctx->ctx, enums_p[u].ref, e->ref);
Radek Krejci693262f2019-04-29 15:23:20 +02001817 e->flags = enums_p[u].flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01001818 if (base_enums) {
1819 /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
1820 LY_ARRAY_FOR(base_enums, v) {
1821 if (!strcmp(e->name, base_enums[v].name)) {
1822 break;
1823 }
1824 }
1825 if (v == LY_ARRAY_SIZE(base_enums)) {
1826 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1827 "Invalid %s - derived type adds new item \"%s\".",
1828 basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name);
1829 return LY_EVALID;
1830 }
1831 match = v;
1832 }
1833
1834 if (basetype == LY_TYPE_ENUM) {
Radek Krejci693262f2019-04-29 15:23:20 +02001835 e->flags |= LYS_ISENUM;
Radek Krejci19a96102018-11-15 13:38:09 +01001836 if (enums_p[u].flags & LYS_SET_VALUE) {
1837 e->value = (int32_t)enums_p[u].value;
1838 if (!u || e->value >= value) {
1839 value = e->value + 1;
1840 }
1841 /* check collision with other values */
1842 for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) {
1843 if (e->value == (*enums)[v].value) {
1844 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1845 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
1846 e->value, e->name, (*enums)[v].name);
1847 return LY_EVALID;
1848 }
1849 }
1850 } else if (base_enums) {
1851 /* inherit the assigned value */
1852 e->value = base_enums[match].value;
1853 if (!u || e->value >= value) {
1854 value = e->value + 1;
1855 }
1856 } else {
1857 /* assign value automatically */
1858 if (u && value == INT32_MIN) {
1859 /* counter overflow */
1860 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1861 "Invalid enumeration - it is not possible to auto-assign enum value for "
1862 "\"%s\" since the highest value is already 2147483647.", e->name);
1863 return LY_EVALID;
1864 }
1865 e->value = value++;
1866 }
1867 } else { /* LY_TYPE_BITS */
1868 if (enums_p[u].flags & LYS_SET_VALUE) {
1869 e->value = (int32_t)enums_p[u].value;
1870 if (!u || (uint32_t)e->value >= position) {
1871 position = (uint32_t)e->value + 1;
1872 }
1873 /* check collision with other values */
1874 for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) {
1875 if (e->value == (*enums)[v].value) {
1876 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1877 "Invalid bits - position %u collide in items \"%s\" and \"%s\".",
1878 (uint32_t)e->value, e->name, (*enums)[v].name);
1879 return LY_EVALID;
1880 }
1881 }
1882 } else if (base_enums) {
1883 /* inherit the assigned value */
1884 e->value = base_enums[match].value;
1885 if (!u || (uint32_t)e->value >= position) {
1886 position = (uint32_t)e->value + 1;
1887 }
1888 } else {
1889 /* assign value automatically */
1890 if (u && position == 0) {
1891 /* counter overflow */
1892 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1893 "Invalid bits - it is not possible to auto-assign bit position for "
1894 "\"%s\" since the highest value is already 4294967295.", e->name);
1895 return LY_EVALID;
1896 }
1897 e->value = position++;
1898 }
1899 }
1900
1901 if (base_enums) {
1902 /* the assigned values must not change from the derived type */
1903 if (e->value != base_enums[match].value) {
1904 if (basetype == LY_TYPE_ENUM) {
1905 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1906 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
1907 e->name, base_enums[match].value, e->value);
1908 } else {
1909 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1910 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
1911 e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value);
1912 }
1913 return LY_EVALID;
1914 }
1915 }
1916
Radek Krejciec4da802019-05-02 13:02:41 +02001917 COMPILE_ARRAY_GOTO(ctx, enums_p[u].iffeatures, e->iffeatures, v, lys_compile_iffeature, ret, done);
1918 COMPILE_ARRAY_GOTO(ctx, enums_p[u].exts, e->exts, v, lys_compile_ext, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01001919
1920 if (basetype == LY_TYPE_BITS) {
1921 /* keep bits ordered by position */
1922 for (v = u; v && (*enums)[v - 1].value > e->value; --v);
1923 if (v != u) {
1924 memcpy(&storage, e, sizeof *e);
1925 memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums);
1926 memcpy(&(*enums)[v], &storage, sizeof storage);
1927 }
1928 }
1929 }
1930
1931done:
1932 return ret;
1933}
1934
Radek Krejcia3045382018-11-22 14:30:31 +01001935#define MOVE_PATH_PARENT(NODE, LIMIT_COND, TERM, ERR_MSG, ...) \
1936 for ((NODE) = (NODE)->parent; \
1937 (NODE) && !((NODE)->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_ACTION | LYS_NOTIF | LYS_ACTION)); \
1938 (NODE) = (NODE)->parent); \
1939 if (!(NODE) && (LIMIT_COND)) { /* we are going higher than top-level */ \
1940 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, ERR_MSG, ##__VA_ARGS__); \
1941 TERM; \
1942 }
1943
1944/**
1945 * @brief Validate the predicate(s) from the leafref path.
1946 * @param[in] ctx Compile context
1947 * @param[in, out] predicate Pointer to the predicate in the leafref path. The pointer is moved after the validated predicate(s).
1948 * Since there can be multiple adjacent predicates for lists with multiple keys, all such predicates are validated.
Radek Krejci4ce68932018-11-28 12:53:36 +01001949 * @param[in] start_node Path context node (where the path is instantiated).
Radek Krejcia3045382018-11-22 14:30:31 +01001950 * @param[in] context_node Predicate context node (where the predicate is placed).
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001951 * @param[in] path_context Schema where the path was defined to correct resolve of the prefixes.
Radek Krejcia3045382018-11-22 14:30:31 +01001952 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1953 */
1954static LY_ERR
Radek Krejci4ce68932018-11-28 12:53:36 +01001955lys_compile_leafref_predicate_validate(struct lysc_ctx *ctx, const char **predicate, const struct lysc_node *start_node,
Radek Krejcibade82a2018-12-05 10:13:48 +01001956 const struct lysc_node_list *context_node, const struct lys_module *path_context)
Radek Krejcia3045382018-11-22 14:30:31 +01001957{
1958 LY_ERR ret = LY_EVALID;
1959 const struct lys_module *mod;
1960 const struct lysc_node *src_node, *dst_node;
1961 const char *path_key_expr, *pke_start, *src, *src_prefix, *dst, *dst_prefix;
1962 size_t src_len, src_prefix_len, dst_len, dst_prefix_len;
Radek Krejcibade82a2018-12-05 10:13:48 +01001963 unsigned int dest_parent_times, c, u;
Radek Krejcia3045382018-11-22 14:30:31 +01001964 const char *start, *end, *pke_end;
1965 struct ly_set keys = {0};
1966 int i;
1967
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001968 assert(path_context);
1969
Radek Krejcia3045382018-11-22 14:30:31 +01001970 while (**predicate == '[') {
1971 start = (*predicate)++;
1972
1973 while (isspace(**predicate)) {
1974 ++(*predicate);
1975 }
1976 LY_CHECK_GOTO(lys_parse_nodeid(predicate, &src_prefix, &src_prefix_len, &src, &src_len), cleanup);
1977 while (isspace(**predicate)) {
1978 ++(*predicate);
1979 }
1980 if (**predicate != '=') {
1981 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01001982 "Invalid leafref path predicate \"%.*s\" - missing \"=\" after node-identifier.",
1983 *predicate - start + 1, start);
Radek Krejcia3045382018-11-22 14:30:31 +01001984 goto cleanup;
1985 }
1986 ++(*predicate);
1987 while (isspace(**predicate)) {
1988 ++(*predicate);
1989 }
1990
1991 if ((end = pke_end = strchr(*predicate, ']')) == NULL) {
1992 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1993 "Invalid leafref path predicate \"%s\" - missing predicate termination.", start);
1994 goto cleanup;
1995 }
1996 --pke_end;
1997 while (isspace(*pke_end)) {
1998 --pke_end;
1999 }
Radek Krejci7adf4ff2018-12-05 08:55:00 +01002000 ++pke_end;
Radek Krejcia3045382018-11-22 14:30:31 +01002001 /* localize path-key-expr */
2002 pke_start = path_key_expr = *predicate;
2003 /* move after the current predicate */
2004 *predicate = end + 1;
2005
2006 /* source (must be leaf or leaf-list) */
2007 if (src_prefix) {
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002008 mod = lys_module_find_prefix(path_context, src_prefix, src_prefix_len);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002009 if (!mod) {
2010 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2011 "Invalid leafref path predicate \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".",
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002012 *predicate - start, start, src_prefix_len, src_prefix, path_context->name);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002013 goto cleanup;
2014 }
Radek Krejci92cc8512019-04-25 09:57:06 +02002015 if (!mod->implemented) {
2016 /* make the module implemented */
2017 ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2);
2018 }
Radek Krejcia3045382018-11-22 14:30:31 +01002019 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002020 mod = start_node->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002021 }
Radek Krejcibade82a2018-12-05 10:13:48 +01002022 src_node = NULL;
2023 if (context_node->keys) {
2024 for (u = 0; u < LY_ARRAY_SIZE(context_node->keys); ++u) {
2025 if (!strncmp(src, context_node->keys[u]->name, src_len) && context_node->keys[u]->name[src_len] == '\0') {
2026 src_node = (const struct lysc_node*)context_node->keys[u];
2027 break;
2028 }
2029 }
2030 }
Radek Krejcia3045382018-11-22 14:30:31 +01002031 if (!src_node) {
2032 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01002033 "Invalid leafref path predicate \"%.*s\" - predicate's key node \"%.*s\" not found.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002034 *predicate - start, start, src_len, src, mod->name);
Radek Krejcia3045382018-11-22 14:30:31 +01002035 goto cleanup;
2036 }
Radek Krejcia3045382018-11-22 14:30:31 +01002037
2038 /* check that there is only one predicate for the */
Radek Krejcibade82a2018-12-05 10:13:48 +01002039 c = keys.count;
Radek Krejcia3045382018-11-22 14:30:31 +01002040 i = ly_set_add(&keys, (void*)src_node, 0);
2041 LY_CHECK_GOTO(i == -1, cleanup);
Radek Krejcibade82a2018-12-05 10:13:48 +01002042 if (keys.count == c) { /* node was already present in the set */
Radek Krejcia3045382018-11-22 14:30:31 +01002043 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01002044 "Invalid leafref path predicate \"%.*s\" - multiple equality tests for the key \"%s\".",
Radek Krejcia3045382018-11-22 14:30:31 +01002045 *predicate - start, start, src_node->name);
2046 goto cleanup;
2047 }
2048
2049 /* destination */
2050 dest_parent_times = 0;
Radek Krejci4ce68932018-11-28 12:53:36 +01002051 dst_node = start_node;
Radek Krejcia3045382018-11-22 14:30:31 +01002052
2053 /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */
2054 if (strncmp(path_key_expr, "current()", 9)) {
2055 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2056 "Invalid leafref path predicate \"%.*s\" - missing current-function-invocation.",
2057 *predicate - start, start);
2058 goto cleanup;
2059 }
2060 path_key_expr += 9;
2061 while (isspace(*path_key_expr)) {
2062 ++path_key_expr;
2063 }
2064
2065 if (*path_key_expr != '/') {
2066 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2067 "Invalid leafref path predicate \"%.*s\" - missing \"/\" after current-function-invocation.",
2068 *predicate - start, start);
2069 goto cleanup;
2070 }
2071 ++path_key_expr;
2072 while (isspace(*path_key_expr)) {
2073 ++path_key_expr;
2074 }
2075
2076 /* rel-path-keyexpr:
2077 * 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier */
2078 while (!strncmp(path_key_expr, "..", 2)) {
2079 ++dest_parent_times;
2080 path_key_expr += 2;
2081 while (isspace(*path_key_expr)) {
2082 ++path_key_expr;
2083 }
2084 if (*path_key_expr != '/') {
2085 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2086 "Invalid leafref path predicate \"%.*s\" - missing \"/\" in \"../\" rel-path-keyexpr pattern.",
2087 *predicate - start, start);
2088 goto cleanup;
2089 }
2090 ++path_key_expr;
2091 while (isspace(*path_key_expr)) {
2092 ++path_key_expr;
2093 }
2094
2095 /* path is supposed to be evaluated in data tree, so we have to skip
2096 * all schema nodes that cannot be instantiated in data tree */
2097 MOVE_PATH_PARENT(dst_node, !strncmp(path_key_expr, "..", 2), goto cleanup,
2098 "Invalid leafref path predicate \"%.*s\" - too many \"..\" in rel-path-keyexpr.",
2099 *predicate - start, start);
2100 }
2101 if (!dest_parent_times) {
2102 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2103 "Invalid leafref path predicate \"%.*s\" - at least one \"..\" is expected in rel-path-keyexpr.",
2104 *predicate - start, start);
2105 goto cleanup;
2106 }
2107 if (path_key_expr == pke_end) {
2108 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2109 "Invalid leafref path predicate \"%.*s\" - at least one node-identifier is expected in rel-path-keyexpr.",
2110 *predicate - start, start);
2111 goto cleanup;
2112 }
2113
2114 while(path_key_expr != pke_end) {
2115 if (lys_parse_nodeid(&path_key_expr, &dst_prefix, &dst_prefix_len, &dst, &dst_len)) {
2116 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcibade82a2018-12-05 10:13:48 +01002117 "Invalid node identifier in leafref path predicate - character %d (of %.*s).",
2118 path_key_expr - start + 1, *predicate - start, start);
Radek Krejcia3045382018-11-22 14:30:31 +01002119 goto cleanup;
2120 }
2121
2122 if (dst_prefix) {
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002123 mod = lys_module_find_prefix(path_context, dst_prefix, dst_prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01002124 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002125 mod = start_node->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002126 }
2127 if (!mod) {
2128 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci92cc8512019-04-25 09:57:06 +02002129 "Invalid leafref path predicate \"%.*s\" - unable to find module of the node \"%.*s\" in rel-path-keyexpr.",
Radek Krejcia3045382018-11-22 14:30:31 +01002130 *predicate - start, start, dst_len, dst);
2131 goto cleanup;
2132 }
Radek Krejci92cc8512019-04-25 09:57:06 +02002133 if (!mod->implemented) {
2134 /* make the module implemented */
2135 ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2);
2136 }
Radek Krejcia3045382018-11-22 14:30:31 +01002137
2138 dst_node = lys_child(dst_node, mod, dst, dst_len, 0, LYS_GETNEXT_NOSTATECHECK);
2139 if (!dst_node) {
2140 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci92cc8512019-04-25 09:57:06 +02002141 "Invalid leafref path predicate \"%.*s\" - unable to find node \"%.*s\" in the rel-path-keyexpr.",
Radek Krejcia3045382018-11-22 14:30:31 +01002142 *predicate - start, start, path_key_expr - pke_start, pke_start);
2143 goto cleanup;
2144 }
2145 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002146 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 +01002147 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci92cc8512019-04-25 09:57:06 +02002148 "Invalid leafref path predicate \"%.*s\" - rel-path-keyexpr \"%.*s\" refers %s instead of leaf.",
Radek Krejcia3045382018-11-22 14:30:31 +01002149 *predicate - start, start, path_key_expr - pke_start, pke_start, lys_nodetype2str(dst_node->nodetype));
2150 goto cleanup;
2151 }
2152 }
2153
2154 ret = LY_SUCCESS;
2155cleanup:
2156 ly_set_erase(&keys, NULL);
2157 return ret;
2158}
2159
2160/**
2161 * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function.
2162 *
2163 * path-arg = absolute-path / relative-path
2164 * absolute-path = 1*("/" (node-identifier *path-predicate))
2165 * relative-path = 1*(".." "/") descendant-path
2166 *
2167 * @param[in,out] path Path to parse.
2168 * @param[out] prefix Prefix of the token, NULL if there is not any.
2169 * @param[out] pref_len Length of the prefix, 0 if there is not any.
2170 * @param[out] name Name of the token.
2171 * @param[out] nam_len Length of the name.
2172 * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call,
2173 * must not be changed between consecutive calls. -1 if the
2174 * path is absolute.
2175 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
2176 * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path.
2177 */
2178static LY_ERR
2179lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len,
2180 int *parent_times, int *has_predicate)
2181{
2182 int par_times = 0;
2183
2184 assert(path && *path);
2185 assert(parent_times);
2186 assert(prefix);
2187 assert(prefix_len);
2188 assert(name);
2189 assert(name_len);
2190 assert(has_predicate);
2191
2192 *prefix = NULL;
2193 *prefix_len = 0;
2194 *name = NULL;
2195 *name_len = 0;
2196 *has_predicate = 0;
2197
2198 if (!*parent_times) {
2199 if (!strncmp(*path, "..", 2)) {
2200 *path += 2;
2201 ++par_times;
2202 while (!strncmp(*path, "/..", 3)) {
2203 *path += 3;
2204 ++par_times;
2205 }
2206 }
2207 if (par_times) {
2208 *parent_times = par_times;
2209 } else {
2210 *parent_times = -1;
2211 }
2212 }
2213
2214 if (**path != '/') {
2215 return LY_EINVAL;
2216 }
2217 /* skip '/' */
2218 ++(*path);
2219
2220 /* node-identifier ([prefix:]name) */
2221 LY_CHECK_RET(lys_parse_nodeid(path, prefix, prefix_len, name, name_len));
2222
2223 if ((**path == '/' && (*path)[1]) || !**path) {
2224 /* path continues by another token or this is the last token */
2225 return LY_SUCCESS;
2226 } else if ((*path)[0] != '[') {
2227 /* unexpected character */
2228 return LY_EINVAL;
2229 } else {
2230 /* predicate starting with [ */
2231 *has_predicate = 1;
2232 return LY_SUCCESS;
2233 }
2234}
2235
2236/**
Radek Krejci58d171e2018-11-23 13:50:55 +01002237 * @brief Check the features used in if-feature statements applicable to the leafref and its target.
2238 *
2239 * The set of features used for target must be a subset of features used for the leafref.
2240 * This is not a perfect, we should compare the truth tables but it could require too much resources
2241 * and RFC 7950 does not require it explicitely, so we simplify that.
2242 *
2243 * @param[in] refnode The leafref node.
2244 * @param[in] target Tha target node of the leafref.
2245 * @return LY_SUCCESS or LY_EVALID;
2246 */
2247static LY_ERR
2248lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target)
2249{
2250 LY_ERR ret = LY_EVALID;
2251 const struct lysc_node *iter;
Radek Krejci58d171e2018-11-23 13:50:55 +01002252 unsigned int u, v, count;
2253 struct ly_set features = {0};
2254
2255 for (iter = refnode; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002256 if (iter->iffeatures) {
2257 LY_ARRAY_FOR(iter->iffeatures, u) {
2258 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2259 LY_CHECK_GOTO(ly_set_add(&features, iter->iffeatures[u].features[v], 0) == -1, cleanup);
Radek Krejci58d171e2018-11-23 13:50:55 +01002260 }
2261 }
2262 }
2263 }
2264
2265 /* we should have, in features set, a superset of features applicable to the target node.
2266 * So when adding features applicable to the target into the features set, we should not be
2267 * able to actually add any new feature, otherwise it is not a subset of features applicable
2268 * to the leafref itself. */
2269 count = features.count;
2270 for (iter = target; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002271 if (iter->iffeatures) {
2272 LY_ARRAY_FOR(iter->iffeatures, u) {
2273 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2274 if ((unsigned int)ly_set_add(&features, iter->iffeatures[u].features[v], 0) >= count) {
Radek Krejci58d171e2018-11-23 13:50:55 +01002275 /* new feature was added (or LY_EMEM) */
2276 goto cleanup;
2277 }
2278 }
2279 }
2280 }
2281 }
2282 ret = LY_SUCCESS;
2283
2284cleanup:
2285 ly_set_erase(&features, NULL);
2286 return ret;
2287}
2288
2289/**
Radek Krejcia3045382018-11-22 14:30:31 +01002290 * @brief Validate the leafref path.
2291 * @param[in] ctx Compile context
2292 * @param[in] startnode Path context node (where the leafref path begins/is placed).
Radek Krejci412ddfa2018-11-23 11:44:11 +01002293 * @param[in] leafref Leafref to validate.
Radek Krejcia3045382018-11-22 14:30:31 +01002294 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2295 */
2296static LY_ERR
Radek Krejci412ddfa2018-11-23 11:44:11 +01002297lys_compile_leafref_validate(struct lysc_ctx *ctx, struct lysc_node *startnode, struct lysc_type_leafref *leafref)
Radek Krejcia3045382018-11-22 14:30:31 +01002298{
2299 const struct lysc_node *node = NULL, *parent = NULL;
2300 const struct lys_module *mod;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002301 struct lysc_type *type;
Radek Krejcia3045382018-11-22 14:30:31 +01002302 const char *id, *prefix, *name;
2303 size_t prefix_len, name_len;
2304 int parent_times = 0, has_predicate;
2305 unsigned int iter, u;
2306 LY_ERR ret = LY_SUCCESS;
2307
2308 assert(ctx);
2309 assert(startnode);
Radek Krejci412ddfa2018-11-23 11:44:11 +01002310 assert(leafref);
2311
Radek Krejcia3045382018-11-22 14:30:31 +01002312 iter = 0;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002313 id = leafref->path;
Radek Krejcia3045382018-11-22 14:30:31 +01002314 while(*id && (ret = lys_path_token(&id, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)) == LY_SUCCESS) {
2315 if (!iter) { /* first iteration */
2316 /* precess ".." in relative paths */
2317 if (parent_times > 0) {
2318 /* move from the context node */
2319 for (u = 0, parent = startnode; u < (unsigned int)parent_times; u++) {
2320 /* path is supposed to be evaluated in data tree, so we have to skip
2321 * all schema nodes that cannot be instantiated in data tree */
2322 MOVE_PATH_PARENT(parent, u < (unsigned int)parent_times - 1, return LY_EVALID,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002323 "Invalid leafref path \"%s\" - too many \"..\" in the path.", leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002324 }
2325 }
2326 }
2327
2328 if (prefix) {
Radek Krejci412ddfa2018-11-23 11:44:11 +01002329 mod = lys_module_find_prefix(leafref->path_context, prefix, prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01002330 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002331 mod = startnode->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002332 }
2333 if (!mod) {
2334 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002335 "Invalid leafref path - unable to find module connected with the prefix of the node \"%.*s\".",
2336 id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002337 return LY_EVALID;
2338 }
Radek Krejci3d929562019-02-21 11:25:55 +01002339 if (!mod->implemented) {
2340 /* make the module implemented */
2341 ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2);
2342 }
Radek Krejcia3045382018-11-22 14:30:31 +01002343
2344 node = lys_child(parent, mod, name, name_len, 0, LYS_GETNEXT_NOSTATECHECK);
2345 if (!node) {
2346 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002347 "Invalid leafref path - unable to find \"%.*s\".", id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002348 return LY_EVALID;
2349 }
2350 parent = node;
2351
2352 if (has_predicate) {
2353 /* we have predicate, so the current result must be list */
2354 if (node->nodetype != LYS_LIST) {
2355 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2356 "Invalid leafref path - node \"%.*s\" is expected to be a list, but it is %s.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01002357 id - leafref->path, leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01002358 return LY_EVALID;
2359 }
2360
Radek Krejcibade82a2018-12-05 10:13:48 +01002361 LY_CHECK_RET(lys_compile_leafref_predicate_validate(ctx, &id, startnode, (struct lysc_node_list*)node, leafref->path_context),
2362 LY_EVALID);
Radek Krejcia3045382018-11-22 14:30:31 +01002363 }
2364
2365 ++iter;
2366 }
2367 if (ret) {
2368 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002369 "Invalid leafref path at character %d (%s).", id - leafref->path + 1, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002370 return LY_EVALID;
2371 }
2372
2373 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
2374 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2375 "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01002376 leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01002377 return LY_EVALID;
2378 }
2379
2380 /* check status */
2381 if (lysc_check_status(ctx, startnode->flags, startnode->module, startnode->name, node->flags, node->module, node->name)) {
2382 return LY_EVALID;
2383 }
2384
Radek Krejcib57cf1e2018-11-23 14:07:05 +01002385 /* check config */
2386 if (leafref->require_instance && (startnode->flags & LYS_CONFIG_W)) {
2387 if (node->flags & LYS_CONFIG_R) {
2388 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2389 "Invalid leafref path \"%s\" - target is supposed to represent configuration data (as the leafref does), but it does not.",
2390 leafref->path);
2391 return LY_EVALID;
2392 }
2393 }
2394
Radek Krejci412ddfa2018-11-23 11:44:11 +01002395 /* store the target's type and check for circular chain of leafrefs */
2396 leafref->realtype = ((struct lysc_node_leaf*)node)->type;
2397 for (type = leafref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref*)type)->realtype) {
2398 if (type == (struct lysc_type*)leafref) {
2399 /* circular chain detected */
2400 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2401 "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", leafref->path);
2402 return LY_EVALID;
2403 }
2404 }
2405
Radek Krejci58d171e2018-11-23 13:50:55 +01002406 /* check if leafref and its target are under common if-features */
2407 if (lys_compile_leafref_features_validate(startnode, node)) {
2408 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2409 "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of features applicable to the leafref itself.",
2410 leafref->path);
2411 return LY_EVALID;
2412 }
2413
Radek Krejcia3045382018-11-22 14:30:31 +01002414 return LY_SUCCESS;
2415}
2416
Radek Krejcicdfecd92018-11-26 11:27:32 +01002417static 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 Krejciec4da802019-05-02 13:02:41 +02002418 struct lysp_type *type_p, struct lysc_type **type, const char **units);
Radek Krejcia3045382018-11-22 14:30:31 +01002419/**
2420 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
2421 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002422 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2423 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2424 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2425 * @param[in] context_name Name of the context node or referencing typedef for logging.
Radek Krejcia3045382018-11-22 14:30:31 +01002426 * @param[in] type_p Parsed type to compile.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002427 * @param[in] module Context module for the leafref path (to correctly resolve prefixes in path)
Radek Krejcia3045382018-11-22 14:30:31 +01002428 * @param[in] basetype Base YANG built-in type of the type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002429 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
2430 * @param[in] base The latest base (compiled) type from which the current type is being derived.
2431 * @param[out] type Newly created type structure with the filled information about the type.
2432 * @return LY_ERR value.
2433 */
Radek Krejci19a96102018-11-15 13:38:09 +01002434static LY_ERR
Radek Krejcicdfecd92018-11-26 11:27:32 +01002435lys_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 Krejciec4da802019-05-02 13:02:41 +02002436 struct lysp_type *type_p, struct lys_module *module, LY_DATA_TYPE basetype, const char *tpdfname,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002437 struct lysc_type *base, struct lysc_type **type)
Radek Krejcic5c27e52018-11-15 14:38:11 +01002438{
2439 LY_ERR ret = LY_SUCCESS;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002440 unsigned int u, v, additional;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002441 struct lysc_type_bin *bin;
2442 struct lysc_type_num *num;
2443 struct lysc_type_str *str;
2444 struct lysc_type_bits *bits;
2445 struct lysc_type_enum *enumeration;
Radek Krejci6cba4292018-11-15 17:33:29 +01002446 struct lysc_type_dec *dec;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002447 struct lysc_type_identityref *idref;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002448 struct lysc_type_union *un, *un_aux;
2449 void *p;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002450
2451 switch (basetype) {
2452 case LY_TYPE_BINARY:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002453 bin = (struct lysc_type_bin*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002454
2455 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002456 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002457 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
2458 base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002459 if (!tpdfname) {
Radek Krejciec4da802019-05-02 13:02:41 +02002460 COMPILE_ARRAY_GOTO(ctx, type_p->length->exts, bin->length->exts, u, lys_compile_ext, ret, done);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002461 }
2462 }
2463
2464 if (tpdfname) {
2465 type_p->compiled = *type;
2466 *type = calloc(1, sizeof(struct lysc_type_bin));
2467 }
2468 break;
2469 case LY_TYPE_BITS:
2470 /* RFC 7950 9.7 - bits */
2471 bits = (struct lysc_type_bits*)(*type);
2472 if (type_p->bits) {
Radek Krejciec4da802019-05-02 13:02:41 +02002473 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002474 base ? (struct lysc_type_bitenum_item*)((struct lysc_type_bits*)base)->bits : NULL,
2475 (struct lysc_type_bitenum_item**)&bits->bits));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002476 }
2477
Radek Krejci555cb5b2018-11-16 14:54:33 +01002478 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002479 /* type derived from bits built-in type must contain at least one bit */
Radek Krejci6cba4292018-11-15 17:33:29 +01002480 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002481 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002482 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002483 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002484 free(*type);
2485 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002486 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002487 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002488 }
2489
2490 if (tpdfname) {
2491 type_p->compiled = *type;
2492 *type = calloc(1, sizeof(struct lysc_type_bits));
2493 }
2494 break;
Radek Krejci6cba4292018-11-15 17:33:29 +01002495 case LY_TYPE_DEC64:
2496 dec = (struct lysc_type_dec*)(*type);
2497
2498 /* RFC 7950 9.3.4 - fraction-digits */
Radek Krejci555cb5b2018-11-16 14:54:33 +01002499 if (!base) {
Radek Krejci643c8242018-11-15 17:51:11 +01002500 if (!type_p->fraction_digits) {
2501 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002502 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Radek Krejci643c8242018-11-15 17:51:11 +01002503 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002504 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Radek Krejci643c8242018-11-15 17:51:11 +01002505 free(*type);
2506 *type = NULL;
2507 }
2508 return LY_EVALID;
2509 }
2510 } else if (type_p->fraction_digits) {
2511 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
Radek Krejci6cba4292018-11-15 17:33:29 +01002512 if (tpdfname) {
Radek Krejci643c8242018-11-15 17:51:11 +01002513 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2514 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
Radek Krejci6cba4292018-11-15 17:33:29 +01002515 tpdfname);
2516 } else {
Radek Krejci643c8242018-11-15 17:51:11 +01002517 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2518 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
Radek Krejci6cba4292018-11-15 17:33:29 +01002519 free(*type);
2520 *type = NULL;
2521 }
2522 return LY_EVALID;
2523 }
2524 dec->fraction_digits = type_p->fraction_digits;
2525
2526 /* RFC 7950 9.2.4 - range */
2527 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002528 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
2529 base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range));
Radek Krejci6cba4292018-11-15 17:33:29 +01002530 if (!tpdfname) {
Radek Krejciec4da802019-05-02 13:02:41 +02002531 COMPILE_ARRAY_GOTO(ctx, type_p->range->exts, dec->range->exts, u, lys_compile_ext, ret, done);
Radek Krejci6cba4292018-11-15 17:33:29 +01002532 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002533 }
2534
2535 if (tpdfname) {
2536 type_p->compiled = *type;
2537 *type = calloc(1, sizeof(struct lysc_type_dec));
2538 }
2539 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002540 case LY_TYPE_STRING:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002541 str = (struct lysc_type_str*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002542
2543 /* RFC 7950 9.4.4 - length */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002544 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002545 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
2546 base ? ((struct lysc_type_str*)base)->length : NULL, &str->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002547 if (!tpdfname) {
Radek Krejciec4da802019-05-02 13:02:41 +02002548 COMPILE_ARRAY_GOTO(ctx, type_p->length->exts, str->length->exts, u, lys_compile_ext, ret, done);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002549 }
2550 } else if (base && ((struct lysc_type_str*)base)->length) {
2551 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length);
2552 }
2553
2554 /* RFC 7950 9.4.5 - pattern */
2555 if (type_p->patterns) {
Radek Krejciec4da802019-05-02 13:02:41 +02002556 LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002557 base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002558 } else if (base && ((struct lysc_type_str*)base)->patterns) {
2559 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns);
2560 }
2561
2562 if (tpdfname) {
2563 type_p->compiled = *type;
2564 *type = calloc(1, sizeof(struct lysc_type_str));
2565 }
2566 break;
2567 case LY_TYPE_ENUM:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002568 enumeration = (struct lysc_type_enum*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002569
2570 /* RFC 7950 9.6 - enum */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002571 if (type_p->enums) {
Radek Krejciec4da802019-05-02 13:02:41 +02002572 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002573 base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002574 }
2575
Radek Krejci555cb5b2018-11-16 14:54:33 +01002576 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002577 /* type derived from enumerations built-in type must contain at least one enum */
Radek Krejci6cba4292018-11-15 17:33:29 +01002578 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002579 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002580 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002581 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002582 free(*type);
2583 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002584 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002585 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002586 }
2587
2588 if (tpdfname) {
2589 type_p->compiled = *type;
2590 *type = calloc(1, sizeof(struct lysc_type_enum));
2591 }
2592 break;
2593 case LY_TYPE_INT8:
2594 case LY_TYPE_UINT8:
2595 case LY_TYPE_INT16:
2596 case LY_TYPE_UINT16:
2597 case LY_TYPE_INT32:
2598 case LY_TYPE_UINT32:
2599 case LY_TYPE_INT64:
2600 case LY_TYPE_UINT64:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002601 num = (struct lysc_type_num*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002602
2603 /* RFC 6020 9.2.4 - range */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002604 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002605 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
2606 base ? ((struct lysc_type_num*)base)->range : NULL, &num->range));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002607 if (!tpdfname) {
Radek Krejciec4da802019-05-02 13:02:41 +02002608 COMPILE_ARRAY_GOTO(ctx, type_p->range->exts, num->range->exts, u, lys_compile_ext, ret, done);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002609 }
2610 }
2611
2612 if (tpdfname) {
2613 type_p->compiled = *type;
2614 *type = calloc(1, sizeof(struct lysc_type_num));
2615 }
2616 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002617 case LY_TYPE_IDENT:
2618 idref = (struct lysc_type_identityref*)(*type);
2619
2620 /* RFC 7950 9.10.2 - base */
2621 if (type_p->bases) {
2622 if (base) {
2623 /* only the directly derived identityrefs can contain base specification */
2624 if (tpdfname) {
2625 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002626 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
Radek Krejci555cb5b2018-11-16 14:54:33 +01002627 tpdfname);
2628 } else {
2629 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002630 "Invalid base substatement for the type not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01002631 free(*type);
2632 *type = NULL;
2633 }
2634 return LY_EVALID;
2635 }
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002636 LY_CHECK_RET(lys_compile_identity_bases(ctx, type_p->bases, NULL, &idref->bases));
Radek Krejci555cb5b2018-11-16 14:54:33 +01002637 }
2638
2639 if (!base && !type_p->flags) {
2640 /* type derived from identityref built-in type must contain at least one base */
2641 if (tpdfname) {
2642 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
2643 } else {
2644 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
2645 free(*type);
2646 *type = NULL;
2647 }
2648 return LY_EVALID;
2649 }
2650
2651 if (tpdfname) {
2652 type_p->compiled = *type;
2653 *type = calloc(1, sizeof(struct lysc_type_identityref));
2654 }
2655 break;
Radek Krejcia3045382018-11-22 14:30:31 +01002656 case LY_TYPE_LEAFREF:
2657 /* RFC 7950 9.9.3 - require-instance */
2658 if (type_p->flags & LYS_SET_REQINST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002659 if (context_mod->mod->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002660 if (tpdfname) {
2661 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2662 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
2663 } else {
2664 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2665 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
2666 free(*type);
2667 *type = NULL;
2668 }
2669 return LY_EVALID;
2670 }
Radek Krejcia3045382018-11-22 14:30:31 +01002671 ((struct lysc_type_leafref*)(*type))->require_instance = type_p->require_instance;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002672 } else if (base) {
2673 /* inherit */
2674 ((struct lysc_type_leafref*)(*type))->require_instance = ((struct lysc_type_leafref*)base)->require_instance;
Radek Krejcia3045382018-11-22 14:30:31 +01002675 } else {
2676 /* default is true */
2677 ((struct lysc_type_leafref*)(*type))->require_instance = 1;
2678 }
2679 if (type_p->path) {
2680 DUP_STRING(ctx->ctx, (void*)type_p->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002681 ((struct lysc_type_leafref*)(*type))->path_context = module;
Radek Krejcia3045382018-11-22 14:30:31 +01002682 } else if (base) {
2683 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)base)->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002684 ((struct lysc_type_leafref*)(*type))->path_context = ((struct lysc_type_leafref*)base)->path_context;
Radek Krejcia3045382018-11-22 14:30:31 +01002685 } else if (tpdfname) {
2686 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
2687 return LY_EVALID;
2688 } else {
2689 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
2690 free(*type);
2691 *type = NULL;
2692 return LY_EVALID;
2693 }
2694 if (tpdfname) {
2695 type_p->compiled = *type;
2696 *type = calloc(1, sizeof(struct lysc_type_leafref));
2697 }
2698 break;
Radek Krejci16c0f822018-11-16 10:46:10 +01002699 case LY_TYPE_INST:
2700 /* RFC 7950 9.9.3 - require-instance */
2701 if (type_p->flags & LYS_SET_REQINST) {
2702 ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance;
2703 } else {
2704 /* default is true */
2705 ((struct lysc_type_instanceid*)(*type))->require_instance = 1;
2706 }
2707
2708 if (tpdfname) {
2709 type_p->compiled = *type;
2710 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2711 }
2712 break;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002713 case LY_TYPE_UNION:
2714 un = (struct lysc_type_union*)(*type);
2715
2716 /* RFC 7950 7.4 - type */
2717 if (type_p->types) {
2718 if (base) {
2719 /* only the directly derived union can contain types specification */
2720 if (tpdfname) {
2721 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2722 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
2723 tpdfname);
2724 } else {
2725 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2726 "Invalid type substatement for the type not directly derived from union built-in type.");
2727 free(*type);
2728 *type = NULL;
2729 }
2730 return LY_EVALID;
2731 }
2732 /* compile the type */
2733 additional = 0;
2734 LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_SIZE(type_p->types), LY_EVALID);
2735 for (u = 0; u < LY_ARRAY_SIZE(type_p->types); ++u) {
Radek Krejciec4da802019-05-02 13:02:41 +02002736 LY_CHECK_RET(lys_compile_type(ctx, context_node_p, context_flags, context_mod, context_name, &type_p->types[u], &un->types[u + additional], NULL));
Radek Krejcicdfecd92018-11-26 11:27:32 +01002737 if (un->types[u + additional]->basetype == LY_TYPE_UNION) {
2738 /* add space for additional types from the union subtype */
2739 un_aux = (struct lysc_type_union *)un->types[u + additional];
2740 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)));
2741 LY_CHECK_ERR_RET(!p, LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
2742 un->types = (void*)((uint32_t*)(p) + 1);
2743
2744 /* copy subtypes of the subtype union */
2745 for (v = 0; v < LY_ARRAY_SIZE(un_aux->types); ++v) {
2746 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
2747 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
2748 un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
2749 LY_CHECK_ERR_RET(!un->types[u + additional], LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
2750 ((struct lysc_type_leafref*)un->types[u + additional])->basetype = LY_TYPE_LEAFREF;
2751 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)un_aux->types[v])->path, ((struct lysc_type_leafref*)un->types[u + additional])->path);
2752 ((struct lysc_type_leafref*)un->types[u + additional])->refcount = 1;
2753 ((struct lysc_type_leafref*)un->types[u + additional])->require_instance = ((struct lysc_type_leafref*)un_aux->types[v])->require_instance;
2754 ((struct lysc_type_leafref*)un->types[u + additional])->path_context = ((struct lysc_type_leafref*)un_aux->types[v])->path_context;
2755 /* TODO extensions */
2756
2757 } else {
2758 un->types[u + additional] = un_aux->types[v];
2759 ++un_aux->types[v]->refcount;
2760 }
2761 ++additional;
2762 LY_ARRAY_INCREMENT(un->types);
2763 }
2764 /* compensate u increment in main loop */
2765 --additional;
2766
2767 /* free the replaced union subtype */
2768 lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux);
2769 } else {
2770 LY_ARRAY_INCREMENT(un->types);
2771 }
Radek Krejcicdfecd92018-11-26 11:27:32 +01002772 }
2773 }
2774
2775 if (!base && !type_p->flags) {
2776 /* type derived from union built-in type must contain at least one type */
2777 if (tpdfname) {
2778 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
2779 } else {
2780 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
2781 free(*type);
2782 *type = NULL;
2783 }
2784 return LY_EVALID;
2785 }
2786
2787 if (tpdfname) {
2788 type_p->compiled = *type;
2789 *type = calloc(1, sizeof(struct lysc_type_union));
2790 }
2791 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002792 case LY_TYPE_BOOL:
2793 case LY_TYPE_EMPTY:
2794 case LY_TYPE_UNKNOWN: /* just to complete switch */
2795 break;
2796 }
2797 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
2798done:
2799 return ret;
2800}
2801
Radek Krejcia3045382018-11-22 14:30:31 +01002802/**
2803 * @brief Compile information about the leaf/leaf-list's type.
2804 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002805 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2806 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2807 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2808 * @param[in] context_name Name of the context node or referencing typedef for logging.
2809 * @param[in] type_p Parsed type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002810 * @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 +01002811 * @param[out] units Storage for inheriting units value from the typedefs the current type derives from.
Radek Krejcia3045382018-11-22 14:30:31 +01002812 * @return LY_ERR value.
2813 */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002814static LY_ERR
Radek Krejcicdfecd92018-11-26 11:27:32 +01002815lys_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 Krejciec4da802019-05-02 13:02:41 +02002816 struct lysp_type *type_p, struct lysc_type **type, const char **units)
Radek Krejci19a96102018-11-15 13:38:09 +01002817{
2818 LY_ERR ret = LY_SUCCESS;
2819 unsigned int u;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002820 int dummyloops = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002821 struct type_context {
2822 const struct lysp_tpdf *tpdf;
2823 struct lysp_node *node;
2824 struct lysp_module *mod;
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002825 } *tctx, *tctx_prev = NULL, *tctx_iter;
Radek Krejci19a96102018-11-15 13:38:09 +01002826 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002827 struct lysc_type *base = NULL, *prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01002828 struct ly_set tpdf_chain = {0};
Radek Krejci01342af2019-01-03 15:18:08 +01002829 const char *dflt = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +01002830
2831 (*type) = NULL;
2832
2833 tctx = calloc(1, sizeof *tctx);
2834 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +01002835 for (ret = lysp_type_find(type_p->name, context_node_p, ctx->mod_def->parsed,
Radek Krejci19a96102018-11-15 13:38:09 +01002836 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod);
2837 ret == LY_SUCCESS;
2838 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod,
2839 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) {
2840 if (basetype) {
2841 break;
2842 }
2843
2844 /* check status */
Radek Krejcicdfecd92018-11-26 11:27:32 +01002845 ret = lysc_check_status(ctx, context_flags, context_mod, context_name,
2846 tctx->tpdf->flags, tctx->mod, tctx->node ? tctx->node->name : tctx->tpdf->name);
Radek Krejci19a96102018-11-15 13:38:09 +01002847 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
2848
Radek Krejcicdfecd92018-11-26 11:27:32 +01002849 if (units && !*units) {
2850 /* inherit units */
2851 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units);
2852 }
Radek Krejci01342af2019-01-03 15:18:08 +01002853 if (!dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002854 /* inherit default */
Radek Krejci01342af2019-01-03 15:18:08 +01002855 dflt = tctx->tpdf->dflt;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002856 }
Radek Krejci01342af2019-01-03 15:18:08 +01002857 if (dummyloops && (!units || *units) && dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002858 basetype = ((struct type_context*)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
2859 break;
2860 }
2861
Radek Krejci19a96102018-11-15 13:38:09 +01002862 if (tctx->tpdf->type.compiled) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002863 /* it is not necessary to continue, the rest of the chain was already compiled,
2864 * but we still may need to inherit default and units values, so start dummy loops */
Radek Krejci19a96102018-11-15 13:38:09 +01002865 basetype = tctx->tpdf->type.compiled->basetype;
2866 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
Radek Krejci01342af2019-01-03 15:18:08 +01002867 if ((units && !*units) || !dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002868 dummyloops = 1;
2869 goto preparenext;
2870 } else {
2871 tctx = NULL;
2872 break;
2873 }
Radek Krejci19a96102018-11-15 13:38:09 +01002874 }
2875
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002876 /* circular typedef reference detection */
2877 for (u = 0; u < tpdf_chain.count; u++) {
2878 /* local part */
2879 tctx_iter = (struct type_context*)tpdf_chain.objs[u];
2880 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
2881 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2882 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
2883 free(tctx);
2884 ret = LY_EVALID;
2885 goto cleanup;
2886 }
2887 }
2888 for (u = 0; u < ctx->tpdf_chain.count; u++) {
2889 /* global part for unions corner case */
2890 tctx_iter = (struct type_context*)ctx->tpdf_chain.objs[u];
2891 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
2892 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2893 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
2894 free(tctx);
2895 ret = LY_EVALID;
2896 goto cleanup;
2897 }
2898 }
2899
Radek Krejci19a96102018-11-15 13:38:09 +01002900 /* store information for the following processing */
2901 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
2902
Radek Krejcicdfecd92018-11-26 11:27:32 +01002903preparenext:
Radek Krejci19a96102018-11-15 13:38:09 +01002904 /* prepare next loop */
2905 tctx_prev = tctx;
2906 tctx = calloc(1, sizeof *tctx);
2907 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
2908 }
2909 free(tctx);
2910
2911 /* allocate type according to the basetype */
2912 switch (basetype) {
2913 case LY_TYPE_BINARY:
2914 *type = calloc(1, sizeof(struct lysc_type_bin));
Radek Krejci19a96102018-11-15 13:38:09 +01002915 break;
2916 case LY_TYPE_BITS:
2917 *type = calloc(1, sizeof(struct lysc_type_bits));
Radek Krejci19a96102018-11-15 13:38:09 +01002918 break;
2919 case LY_TYPE_BOOL:
2920 case LY_TYPE_EMPTY:
2921 *type = calloc(1, sizeof(struct lysc_type));
2922 break;
2923 case LY_TYPE_DEC64:
2924 *type = calloc(1, sizeof(struct lysc_type_dec));
2925 break;
2926 case LY_TYPE_ENUM:
2927 *type = calloc(1, sizeof(struct lysc_type_enum));
Radek Krejci19a96102018-11-15 13:38:09 +01002928 break;
2929 case LY_TYPE_IDENT:
2930 *type = calloc(1, sizeof(struct lysc_type_identityref));
2931 break;
2932 case LY_TYPE_INST:
2933 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2934 break;
2935 case LY_TYPE_LEAFREF:
2936 *type = calloc(1, sizeof(struct lysc_type_leafref));
2937 break;
2938 case LY_TYPE_STRING:
2939 *type = calloc(1, sizeof(struct lysc_type_str));
Radek Krejci19a96102018-11-15 13:38:09 +01002940 break;
2941 case LY_TYPE_UNION:
2942 *type = calloc(1, sizeof(struct lysc_type_union));
2943 break;
2944 case LY_TYPE_INT8:
2945 case LY_TYPE_UINT8:
2946 case LY_TYPE_INT16:
2947 case LY_TYPE_UINT16:
2948 case LY_TYPE_INT32:
2949 case LY_TYPE_UINT32:
2950 case LY_TYPE_INT64:
2951 case LY_TYPE_UINT64:
2952 *type = calloc(1, sizeof(struct lysc_type_num));
Radek Krejci19a96102018-11-15 13:38:09 +01002953 break;
2954 case LY_TYPE_UNKNOWN:
2955 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2956 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
2957 ret = LY_EVALID;
2958 goto cleanup;
2959 }
2960 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002961 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci19a96102018-11-15 13:38:09 +01002962 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
2963 ly_data_type2str[basetype]);
2964 free(*type);
2965 (*type) = NULL;
2966 ret = LY_EVALID;
2967 goto cleanup;
2968 }
2969
2970 /* get restrictions from the referred typedefs */
2971 for (u = tpdf_chain.count - 1; u + 1 > 0; --u) {
2972 tctx = (struct type_context*)tpdf_chain.objs[u];
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002973
2974 /* remember the typedef context for circular check */
2975 ly_set_add(&ctx->tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
2976
Radek Krejci43699232018-11-23 14:59:46 +01002977 if (tctx->tpdf->type.compiled) {
Radek Krejci19a96102018-11-15 13:38:09 +01002978 base = tctx->tpdf->type.compiled;
2979 continue;
Radek Krejci43699232018-11-23 14:59:46 +01002980 } else if (basetype != LY_TYPE_LEAFREF && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) {
Radek Krejci19a96102018-11-15 13:38:09 +01002981 /* no change, just use the type information from the base */
2982 base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled;
2983 ++base->refcount;
2984 continue;
2985 }
2986
2987 ++(*type)->refcount;
Radek Krejci43699232018-11-23 14:59:46 +01002988 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
2989 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
2990 tctx->tpdf->name, ly_data_type2str[basetype]);
2991 ret = LY_EVALID;
2992 goto cleanup;
2993 } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) {
2994 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2995 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
2996 tctx->tpdf->name, tctx->tpdf->dflt);
2997 ret = LY_EVALID;
2998 goto cleanup;
2999 }
3000
Radek Krejci19a96102018-11-15 13:38:09 +01003001 (*type)->basetype = basetype;
Radek Krejcic5c27e52018-11-15 14:38:11 +01003002 prev_type = *type;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003003 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 +01003004 basetype & (LY_TYPE_LEAFREF | LY_TYPE_UNION) ? lysp_find_module(ctx->ctx, tctx->mod) : NULL,
Radek Krejciec4da802019-05-02 13:02:41 +02003005 basetype, tctx->tpdf->name, base, type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01003006 LY_CHECK_GOTO(ret, cleanup);
3007 base = prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01003008 }
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003009 /* remove the processed typedef contexts from the stack for circular check */
3010 ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count;
Radek Krejci19a96102018-11-15 13:38:09 +01003011
Radek Krejcic5c27e52018-11-15 14:38:11 +01003012 /* process the type definition in leaf */
Radek Krejcicdfecd92018-11-26 11:27:32 +01003013 if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) {
Radek Krejcia3045382018-11-22 14:30:31 +01003014 /* get restrictions from the node itself */
Radek Krejci19a96102018-11-15 13:38:09 +01003015 (*type)->basetype = basetype;
3016 ++(*type)->refcount;
Radek Krejciec4da802019-05-02 13:02:41 +02003017 ret = lys_compile_type_(ctx, context_node_p, context_flags, context_mod, context_name, type_p, ctx->mod_def, basetype, NULL, base, type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01003018 LY_CHECK_GOTO(ret, cleanup);
3019 } else {
Radek Krejci19a96102018-11-15 13:38:09 +01003020 /* no specific restriction in leaf's type definition, copy from the base */
3021 free(*type);
3022 (*type) = base;
3023 ++(*type)->refcount;
Radek Krejci19a96102018-11-15 13:38:09 +01003024 }
Radek Krejci01342af2019-01-03 15:18:08 +01003025 if (!(*type)->dflt) {
3026 DUP_STRING(ctx->ctx, dflt, (*type)->dflt);
3027 }
Radek Krejci19a96102018-11-15 13:38:09 +01003028
Radek Krejciec4da802019-05-02 13:02:41 +02003029 COMPILE_ARRAY_GOTO(ctx, type_p->exts, (*type)->exts, u, lys_compile_ext, ret, cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01003030
3031cleanup:
3032 ly_set_erase(&tpdf_chain, free);
3033 return ret;
3034}
3035
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003036/**
3037 * @brief Compile status information of the given node.
3038 *
3039 * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes
3040 * has the status correctly set during the compilation.
3041 *
3042 * @param[in] ctx Compile context
3043 * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved.
3044 * If the status was set explicitly on the node, it is already set in the flags value and we just check
3045 * the compatibility with the parent's status value.
3046 * @param[in] parent_flags Flags of the parent node to check/inherit the status value.
3047 * @return LY_ERR value.
3048 */
3049static LY_ERR
3050lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags)
3051{
3052 /* status - it is not inherited by specification, but it does not make sense to have
3053 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3054 if (!((*node_flags) & LYS_STATUS_MASK)) {
3055 if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) {
3056 if ((parent_flags & 0x3) != 0x3) {
3057 /* do not print the warning when inheriting status from uses - the uses_status value has a special
3058 * combination of bits (0x3) which marks the uses_status value */
3059 LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.",
3060 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3061 }
3062 (*node_flags) |= parent_flags & LYS_STATUS_MASK;
3063 } else {
3064 (*node_flags) |= LYS_STATUS_CURR;
3065 }
3066 } else if (parent_flags & LYS_STATUS_MASK) {
3067 /* check status compatibility with the parent */
3068 if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) {
3069 if ((*node_flags) & LYS_STATUS_CURR) {
3070 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3071 "A \"current\" status is in conflict with the parent's \"%s\" status.",
3072 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3073 } else { /* LYS_STATUS_DEPRC */
3074 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3075 "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
3076 }
3077 return LY_EVALID;
3078 }
3079 }
3080 return LY_SUCCESS;
3081}
3082
Radek Krejci8cce8532019-03-05 11:27:45 +01003083/**
3084 * @brief Check uniqness of the node/action/notification name.
3085 *
3086 * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema
3087 * structures, but they share the namespace so we need to check their name collisions.
3088 *
3089 * @param[in] ctx Compile context.
3090 * @param[in] children List (linked list) of data nodes to go through.
3091 * @param[in] actions List (sized array) of actions or RPCs to go through.
3092 * @param[in] notifs List (sized array) of Notifications to go through.
3093 * @param[in] name Name of the item to find in the given lists.
3094 * @param[in] exclude Pointer to an object to exclude from the name checking - for the case that the object
3095 * with the @p name being checked is already inserted into one of the list so we need to skip it when searching for duplicity.
3096 * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise.
3097 */
3098static LY_ERR
3099lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *children,
3100 const struct lysc_action *actions, const struct lysc_notif *notifs,
3101 const char *name, void *exclude)
3102{
3103 const struct lysc_node *iter;
3104 unsigned int u;
3105
3106 LY_LIST_FOR(children, iter) {
3107 if (iter != exclude && iter->module == ctx->mod && !strcmp(name, iter->name)) {
3108 goto error;
3109 }
3110 }
3111 LY_ARRAY_FOR(actions, u) {
3112 if (&actions[u] != exclude && actions[u].module == ctx->mod && !strcmp(name, actions[u].name)) {
3113 goto error;
3114 }
3115 }
3116 LY_ARRAY_FOR(notifs, u) {
3117 if (&notifs[u] != exclude && notifs[u].module == ctx->mod && !strcmp(name, notifs[u].name)) {
3118 goto error;
3119 }
3120 }
3121 return LY_SUCCESS;
3122error:
3123 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/Notification");
3124 return LY_EEXIST;
3125}
3126
Radek Krejciec4da802019-05-02 13:02:41 +02003127static LY_ERR lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *parent, uint16_t uses_status);
Radek Krejci19a96102018-11-15 13:38:09 +01003128
Radek Krejcia3045382018-11-22 14:30:31 +01003129/**
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003130 * @brief Compile parsed RPC/action schema node information.
3131 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003132 * @param[in] action_p Parsed RPC/action schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003133 * @param[in] parent Parent node of the action, NULL in case of RPC (top-level action)
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003134 * @param[in,out] action Prepared (empty) compiled action structure to fill.
3135 * @param[in] uses_status If the RPC/action is being placed instead of uses, here we have the uses's status value (as node's flags).
3136 * Zero means no uses, non-zero value with no status bit set mean the default status.
3137 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3138 */
3139static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003140lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003141 struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status)
3142{
3143 LY_ERR ret = LY_SUCCESS;
3144 struct lysp_node *child_p;
3145 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02003146 int opt_prev = ctx->options;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003147
Radek Krejci8cce8532019-03-05 11:27:45 +01003148 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3149 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3150 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3151 action_p->name, action)) {
3152 return LY_EVALID;
3153 }
3154
Radek Krejciec4da802019-05-02 13:02:41 +02003155 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejci05b774b2019-02-25 13:26:18 +01003156 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003157 "Action \"%s\" is placed inside %s.", action_p->name,
Radek Krejciec4da802019-05-02 13:02:41 +02003158 ctx->options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "Notification");
Radek Krejci05b774b2019-02-25 13:26:18 +01003159 return LY_EVALID;
3160 }
3161
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003162 action->nodetype = LYS_ACTION;
3163 action->module = ctx->mod;
3164 action->parent = parent;
Radek Krejciec4da802019-05-02 13:02:41 +02003165 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003166 action->sp = action_p;
3167 }
3168 action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK;
3169
3170 /* status - it is not inherited by specification, but it does not make sense to have
3171 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3172 LY_CHECK_RET(lys_compile_status(ctx, &action->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
3173
3174 DUP_STRING(ctx->ctx, action_p->name, action->name);
3175 DUP_STRING(ctx->ctx, action_p->dsc, action->dsc);
3176 DUP_STRING(ctx->ctx, action_p->ref, action->ref);
Radek Krejciec4da802019-05-02 13:02:41 +02003177 COMPILE_ARRAY_GOTO(ctx, action_p->iffeatures, action->iffeatures, u, lys_compile_iffeature, ret, cleanup);
3178 COMPILE_ARRAY_GOTO(ctx, action_p->exts, action->exts, u, lys_compile_ext, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003179
3180 /* input */
Radek Krejciec4da802019-05-02 13:02:41 +02003181 COMPILE_ARRAY_GOTO(ctx, action_p->input.musts, action->input.musts, u, lys_compile_must, ret, cleanup);
3182 COMPILE_ARRAY_GOTO(ctx, action_p->input.exts, action->input_exts, u, lys_compile_ext, ret, cleanup);
3183 ctx->options |= LYSC_OPT_RPC_INPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003184 LY_LIST_FOR(action_p->input.data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003185 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003186 }
Radek Krejciec4da802019-05-02 13:02:41 +02003187 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003188
3189 /* output */
Radek Krejciec4da802019-05-02 13:02:41 +02003190 COMPILE_ARRAY_GOTO(ctx, action_p->output.musts, action->output.musts, u, lys_compile_must, ret, cleanup);
3191 COMPILE_ARRAY_GOTO(ctx, action_p->output.exts, action->output_exts, u, lys_compile_ext, ret, cleanup);
3192 ctx->options |= LYSC_OPT_RPC_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003193 LY_LIST_FOR(action_p->output.data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003194 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003195 }
3196
3197cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003198 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003199 return ret;
3200}
3201
3202/**
Radek Krejci43981a32019-04-12 09:44:11 +02003203 * @brief Compile parsed Notification schema node information.
Radek Krejcifc11bd72019-04-11 16:00:05 +02003204 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003205 * @param[in] notif_p Parsed Notification schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003206 * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification
3207 * @param[in,out] notif Prepared (empty) compiled notification structure to fill.
3208 * @param[in] uses_status If the Notification is being placed instead of uses, here we have the uses's status value (as node's flags).
Radek Krejcifc11bd72019-04-11 16:00:05 +02003209 * Zero means no uses, non-zero value with no status bit set mean the default status.
3210 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3211 */
3212static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003213lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003214 struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status)
3215{
3216 LY_ERR ret = LY_SUCCESS;
3217 struct lysp_node *child_p;
3218 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02003219 int opt_prev = ctx->options;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003220
3221 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3222 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3223 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3224 notif_p->name, notif)) {
3225 return LY_EVALID;
3226 }
3227
Radek Krejciec4da802019-05-02 13:02:41 +02003228 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003229 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3230 "Notification \"%s\" is placed inside %s.", notif_p->name,
Radek Krejciec4da802019-05-02 13:02:41 +02003231 ctx->options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another Notification");
Radek Krejcifc11bd72019-04-11 16:00:05 +02003232 return LY_EVALID;
3233 }
3234
3235 notif->nodetype = LYS_NOTIF;
3236 notif->module = ctx->mod;
3237 notif->parent = parent;
Radek Krejciec4da802019-05-02 13:02:41 +02003238 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003239 notif->sp = notif_p;
3240 }
3241 notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK;
3242
3243 /* status - it is not inherited by specification, but it does not make sense to have
3244 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3245 LY_CHECK_RET(lys_compile_status(ctx, &notif->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
3246
3247 DUP_STRING(ctx->ctx, notif_p->name, notif->name);
3248 DUP_STRING(ctx->ctx, notif_p->dsc, notif->dsc);
3249 DUP_STRING(ctx->ctx, notif_p->ref, notif->ref);
Radek Krejciec4da802019-05-02 13:02:41 +02003250 COMPILE_ARRAY_GOTO(ctx, notif_p->iffeatures, notif->iffeatures, u, lys_compile_iffeature, ret, cleanup);
3251 COMPILE_ARRAY_GOTO(ctx, notif_p->exts, notif->exts, u, lys_compile_ext, ret, cleanup);
3252 COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, u, lys_compile_must, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003253
Radek Krejciec4da802019-05-02 13:02:41 +02003254 ctx->options |= LYSC_OPT_NOTIFICATION;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003255 LY_LIST_FOR(notif_p->data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003256 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)notif, uses_status));
Radek Krejcifc11bd72019-04-11 16:00:05 +02003257 }
3258
3259cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003260 ctx->options = opt_prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003261 return ret;
3262}
3263
3264/**
Radek Krejcia3045382018-11-22 14:30:31 +01003265 * @brief Compile parsed container node information.
3266 * @param[in] ctx Compile context
3267 * @param[in] node_p Parsed container node.
Radek Krejcia3045382018-11-22 14:30:31 +01003268 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3269 * is enriched with the container-specific information.
3270 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3271 */
Radek Krejci19a96102018-11-15 13:38:09 +01003272static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003273lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003274{
3275 struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p;
3276 struct lysc_node_container *cont = (struct lysc_node_container*)node;
3277 struct lysp_node *child_p;
3278 unsigned int u;
3279 LY_ERR ret = LY_SUCCESS;
3280
Radek Krejcife909632019-02-12 15:34:42 +01003281 if (cont_p->presence) {
3282 cont->flags |= LYS_PRESENCE;
3283 }
3284
Radek Krejci19a96102018-11-15 13:38:09 +01003285 LY_LIST_FOR(cont_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003286 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci19a96102018-11-15 13:38:09 +01003287 }
3288
Radek Krejciec4da802019-05-02 13:02:41 +02003289 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, u, lys_compile_must, ret, done);
3290 COMPILE_ARRAY1_GOTO(ctx, cont_p->actions, cont->actions, node, u, lys_compile_action, 0, ret, done);
3291 COMPILE_ARRAY1_GOTO(ctx, cont_p->notifs, cont->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01003292
3293done:
3294 return ret;
3295}
3296
Radek Krejci33f72892019-02-21 10:36:58 +01003297/*
3298 * @brief Compile type in leaf/leaf-list node and do all the necessary checks.
3299 * @param[in] ctx Compile context.
3300 * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types.
3301 * @param[in] type_p Parsed type to compile.
Radek Krejci33f72892019-02-21 10:36:58 +01003302 * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information.
3303 * @return LY_ERR value.
3304 */
3305static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003306lys_compile_node_type(struct lysc_ctx *ctx, struct lysp_node *context_node, struct lysp_type *type_p, struct lysc_node_leaf *leaf)
Radek Krejci33f72892019-02-21 10:36:58 +01003307{
3308 unsigned int u, v;
3309 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)leaf;
3310
Radek Krejciec4da802019-05-02 13:02:41 +02003311 LY_CHECK_RET(lys_compile_type(ctx, context_node, leaf->flags, ctx->mod_def->parsed, leaf->name, type_p, &leaf->type,
Radek Krejci33f72892019-02-21 10:36:58 +01003312 leaf->units ? NULL : &leaf->units));
3313 if (leaf->nodetype == LYS_LEAFLIST) {
3314 if (llist->type->dflt && !llist->dflts && !llist->min) {
3315 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, 1, LY_EMEM);
3316 DUP_STRING(ctx->ctx, llist->type->dflt, llist->dflts[0]);
3317 LY_ARRAY_INCREMENT(llist->dflts);
3318 }
3319 } else {
3320 if (leaf->type->dflt && !leaf->dflt && !(leaf->flags & LYS_MAND_TRUE)) {
3321 DUP_STRING(ctx->ctx, leaf->type->dflt, leaf->dflt);
3322 }
3323 }
3324 if (leaf->type->basetype == LY_TYPE_LEAFREF) {
3325 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
3326 ly_set_add(&ctx->unres, leaf, 0);
3327 } else if (leaf->type->basetype == LY_TYPE_UNION) {
3328 LY_ARRAY_FOR(((struct lysc_type_union*)leaf->type)->types, u) {
3329 if (((struct lysc_type_union*)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
3330 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
3331 ly_set_add(&ctx->unres, leaf, 0);
3332 }
3333 }
3334 } else if (leaf->type->basetype == LY_TYPE_EMPTY) {
3335 if (leaf->flags & LYS_SET_DFLT) {
3336 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "%s of type \"empty\" must not have a default value (%s).",
3337 leaf->nodetype == LYS_LEAFLIST ? "Leaf-list" : "Leaf", leaf->nodetype == LYS_LEAFLIST ? llist->dflts[0] : leaf->dflt);
3338 return LY_EVALID;
3339 }
3340 if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) {
3341 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3342 "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
3343 return LY_EVALID;
3344 }
3345 }
3346
3347 if (leaf->nodetype == LYS_LEAFLIST && (llist->flags & LYS_CONFIG_W) && llist->dflts && LY_ARRAY_SIZE(llist->dflts)) {
3348 /* configuration data values must be unique - so check the default values */
3349 LY_ARRAY_FOR(llist->dflts, u) {
3350 for (v = u + 1; v < LY_ARRAY_SIZE(llist->dflts); ++v) {
3351 if (!strcmp(llist->dflts[u], llist->dflts[v])) {
3352 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3353 "Configuration leaf-list has multiple defaults of the same value \"%s\".", llist->dflts[v]);
3354 return LY_EVALID;
3355 }
3356 }
3357 }
3358 }
3359
3360 /* TODO validate default value according to the type, possibly postpone the check when the leafref target is known */
3361
3362 return LY_SUCCESS;
3363}
3364
Radek Krejcia3045382018-11-22 14:30:31 +01003365/**
3366 * @brief Compile parsed leaf node information.
3367 * @param[in] ctx Compile context
3368 * @param[in] node_p Parsed leaf node.
Radek Krejcia3045382018-11-22 14:30:31 +01003369 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3370 * is enriched with the leaf-specific information.
3371 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3372 */
Radek Krejci19a96102018-11-15 13:38:09 +01003373static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003374lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003375{
3376 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p;
3377 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
3378 unsigned int u;
3379 LY_ERR ret = LY_SUCCESS;
3380
Radek Krejciec4da802019-05-02 13:02:41 +02003381 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, u, lys_compile_must, ret, done);
Radek Krejciccd20f12019-02-15 14:12:27 +01003382 if (leaf_p->units) {
3383 leaf->units = lydict_insert(ctx->ctx, leaf_p->units, 0);
3384 leaf->flags |= LYS_SET_UNITS;
3385 }
3386 if (leaf_p->dflt) {
3387 leaf->dflt = lydict_insert(ctx->ctx, leaf_p->dflt, 0);
Radek Krejci76b3e962018-12-14 17:01:25 +01003388 leaf->flags |= LYS_SET_DFLT;
3389 }
Radek Krejci43699232018-11-23 14:59:46 +01003390
Radek Krejciec4da802019-05-02 13:02:41 +02003391 ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, leaf);
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01003392
Radek Krejci19a96102018-11-15 13:38:09 +01003393done:
3394 return ret;
3395}
3396
Radek Krejcia3045382018-11-22 14:30:31 +01003397/**
Radek Krejci0e5d8382018-11-28 16:37:53 +01003398 * @brief Compile parsed leaf-list node information.
3399 * @param[in] ctx Compile context
3400 * @param[in] node_p Parsed leaf-list node.
Radek Krejci0e5d8382018-11-28 16:37:53 +01003401 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3402 * is enriched with the leaf-list-specific information.
3403 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3404 */
3405static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003406lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci0e5d8382018-11-28 16:37:53 +01003407{
3408 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p;
3409 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
Radek Krejci33f72892019-02-21 10:36:58 +01003410 unsigned int u;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003411 LY_ERR ret = LY_SUCCESS;
3412
Radek Krejciec4da802019-05-02 13:02:41 +02003413 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, u, lys_compile_must, ret, done);
Radek Krejciccd20f12019-02-15 14:12:27 +01003414 if (llist_p->units) {
3415 llist->units = lydict_insert(ctx->ctx, llist_p->units, 0);
3416 llist->flags |= LYS_SET_UNITS;
3417 }
Radek Krejci0e5d8382018-11-28 16:37:53 +01003418
3419 if (llist_p->dflts) {
3420 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(llist_p->dflts), ret, done);
3421 LY_ARRAY_FOR(llist_p->dflts, u) {
3422 DUP_STRING(ctx->ctx, llist_p->dflts[u], llist->dflts[u]);
3423 LY_ARRAY_INCREMENT(llist->dflts);
3424 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003425 llist->flags |= LYS_SET_DFLT;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003426 }
3427
3428 llist->min = llist_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003429 if (llist->min) {
3430 llist->flags |= LYS_MAND_TRUE;
3431 }
Radek Krejcib7408632018-11-28 17:12:11 +01003432 llist->max = llist_p->max ? llist_p->max : (uint32_t)-1;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003433
Radek Krejciec4da802019-05-02 13:02:41 +02003434 ret = lys_compile_node_type(ctx, node_p, &llist_p->type, (struct lysc_node_leaf*)llist);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003435
3436done:
3437 return ret;
3438}
3439
3440/**
Radek Krejci7af64242019-02-18 13:07:53 +01003441 * @brief Compile information about list's uniques.
3442 * @param[in] ctx Compile context.
3443 * @param[in] context_module Module where the prefixes are going to be resolved.
3444 * @param[in] uniques Sized array list of unique statements.
3445 * @param[in] list Compiled list where the uniques are supposed to be resolved and stored.
3446 * @return LY_ERR value.
3447 */
3448static LY_ERR
3449lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list)
3450{
3451 LY_ERR ret = LY_SUCCESS;
3452 struct lysc_node_leaf **key, ***unique;
3453 const char *keystr, *delim;
3454 size_t len;
3455 unsigned int v;
3456 int config;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003457 uint16_t flags;
Radek Krejci7af64242019-02-18 13:07:53 +01003458
3459 for (v = 0; v < LY_ARRAY_SIZE(uniques); ++v) {
3460 config = -1;
3461 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
3462 keystr = uniques[v];
3463 while (keystr) {
3464 delim = strpbrk(keystr, " \t\n");
3465 if (delim) {
3466 len = delim - keystr;
3467 while (isspace(*delim)) {
3468 ++delim;
3469 }
3470 } else {
3471 len = strlen(keystr);
3472 }
3473
3474 /* unique node must be present */
3475 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003476 ret = lys_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node*)list, context_module, LYS_LEAF, 0,
3477 (const struct lysc_node**)key, &flags);
Radek Krejci7af64242019-02-18 13:07:53 +01003478 if (ret != LY_SUCCESS) {
3479 if (ret == LY_EDENIED) {
3480 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003481 "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.",
Radek Krejci7af64242019-02-18 13:07:53 +01003482 len, keystr, lys_nodetype2str((*key)->nodetype));
3483 }
3484 return LY_EVALID;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003485 } else if (flags) {
3486 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3487 "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.",
3488 len, keystr, flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action");
3489 return LY_EVALID;
Radek Krejci7af64242019-02-18 13:07:53 +01003490 }
3491
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003492
Radek Krejci7af64242019-02-18 13:07:53 +01003493 /* all referenced leafs must be of the same config type */
3494 if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) {
3495 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3496 "Unique statement \"%s\" refers to leafs with different config type.", uniques[v]);
3497 return LY_EVALID;
3498 } else if ((*key)->flags & LYS_CONFIG_W) {
3499 config = 1;
3500 } else { /* LYS_CONFIG_R */
3501 config = 0;
3502 }
3503
3504 /* check status */
3505 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3506 (*key)->flags, (*key)->module, (*key)->name));
3507
3508 /* mark leaf as unique */
3509 (*key)->flags |= LYS_UNIQUE;
3510
3511 /* next unique value in line */
3512 keystr = delim;
3513 }
3514 /* next unique definition */
3515 }
3516
3517 return LY_SUCCESS;
3518}
3519
3520/**
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003521 * @brief Compile parsed list node information.
3522 * @param[in] ctx Compile context
3523 * @param[in] node_p Parsed list node.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003524 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3525 * is enriched with the list-specific information.
3526 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3527 */
3528static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003529lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003530{
3531 struct lysp_node_list *list_p = (struct lysp_node_list*)node_p;
3532 struct lysc_node_list *list = (struct lysc_node_list*)node;
3533 struct lysp_node *child_p;
Radek Krejci7af64242019-02-18 13:07:53 +01003534 struct lysc_node_leaf **key;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003535 size_t len;
Radek Krejci7af64242019-02-18 13:07:53 +01003536 unsigned int u;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003537 const char *keystr, *delim;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003538 LY_ERR ret = LY_SUCCESS;
3539
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003540 list->min = list_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003541 if (list->min) {
3542 list->flags |= LYS_MAND_TRUE;
3543 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003544 list->max = list_p->max ? list_p->max : (uint32_t)-1;
3545
3546 LY_LIST_FOR(list_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003547 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003548 }
3549
Radek Krejciec4da802019-05-02 13:02:41 +02003550 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, u, lys_compile_must, ret, done);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003551
3552 /* keys */
3553 if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) {
3554 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
3555 return LY_EVALID;
3556 }
3557
3558 /* find all the keys (must be direct children) */
3559 keystr = list_p->key;
3560 while (keystr) {
3561 delim = strpbrk(keystr, " \t\n");
3562 if (delim) {
3563 len = delim - keystr;
3564 while (isspace(*delim)) {
3565 ++delim;
3566 }
3567 } else {
3568 len = strlen(keystr);
3569 }
3570
3571 /* key node must be present */
3572 LY_ARRAY_NEW_RET(ctx->ctx, list->keys, key, LY_EMEM);
3573 *key = (struct lysc_node_leaf*)lys_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE | LYS_GETNEXT_NOSTATECHECK);
3574 if (!(*key)) {
3575 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3576 "The list's key \"%.*s\" not found.", len, keystr);
3577 return LY_EVALID;
3578 }
3579 /* keys must be unique */
3580 for(u = 0; u < LY_ARRAY_SIZE(list->keys) - 1; ++u) {
3581 if (*key == list->keys[u]) {
3582 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3583 "Duplicated key identifier \"%.*s\".", len, keystr);
3584 return LY_EVALID;
3585 }
3586 }
3587 /* key must have the same config flag as the list itself */
3588 if ((list->flags & LYS_CONFIG_MASK) != ((*key)->flags & LYS_CONFIG_MASK)) {
3589 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf.");
3590 return LY_EVALID;
3591 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003592 if (ctx->mod_def->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003593 /* YANG 1.0 denies key to be of empty type */
3594 if ((*key)->type->basetype == LY_TYPE_EMPTY) {
3595 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3596 "Key of a list can be of type \"empty\" only in YANG 1.1 modules.");
3597 return LY_EVALID;
3598 }
3599 } else {
3600 /* when and if-feature are illegal on list keys */
3601 if ((*key)->when) {
3602 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3603 "List's key \"%s\" must not have any \"when\" statement.", (*key)->name);
3604 return LY_EVALID;
3605 }
3606 if ((*key)->iffeatures) {
3607 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3608 "List's key \"%s\" must not have any \"if-feature\" statement.", (*key)->name);
3609 return LY_EVALID;
3610 }
3611 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003612
3613 /* check status */
3614 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3615 (*key)->flags, (*key)->module, (*key)->name));
3616
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003617 /* ignore default values of the key */
3618 if ((*key)->dflt) {
3619 lydict_remove(ctx->ctx, (*key)->dflt);
3620 (*key)->dflt = NULL;
3621 }
3622 /* mark leaf as key */
3623 (*key)->flags |= LYS_KEY;
3624
3625 /* next key value */
3626 keystr = delim;
3627 }
3628
3629 /* uniques */
3630 if (list_p->uniques) {
Radek Krejci7af64242019-02-18 13:07:53 +01003631 LY_CHECK_RET(lys_compile_node_list_unique(ctx, list->module, list_p->uniques, list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003632 }
3633
Radek Krejciec4da802019-05-02 13:02:41 +02003634 COMPILE_ARRAY1_GOTO(ctx, list_p->actions, list->actions, node, u, lys_compile_action, 0, ret, done);
3635 COMPILE_ARRAY1_GOTO(ctx, list_p->notifs, list->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003636
3637done:
3638 return ret;
3639}
3640
Radek Krejcib56c7502019-02-13 14:19:54 +01003641/**
3642 * @brief Do some checks and set the default choice's case.
3643 *
3644 * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it.
3645 *
3646 * @param[in] ctx Compile context.
3647 * @param[in] dflt Name of the default branch. Can contain even the prefix, but it make sense only in case it is the prefix of the module itself,
3648 * not the reference to the imported module.
3649 * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice.
3650 * @return LY_ERR value.
3651 */
Radek Krejci76b3e962018-12-14 17:01:25 +01003652static LY_ERR
3653lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
3654{
3655 struct lysc_node *iter, *node = (struct lysc_node*)ch;
3656 const char *prefix = NULL, *name;
3657 size_t prefix_len = 0;
3658
3659 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3660 name = strchr(dflt, ':');
3661 if (name) {
3662 prefix = dflt;
3663 prefix_len = name - prefix;
3664 ++name;
3665 } else {
3666 name = dflt;
3667 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003668 if (prefix && (strncmp(prefix, node->module->prefix, prefix_len) || node->module->prefix[prefix_len] != '\0')) {
Radek Krejci76b3e962018-12-14 17:01:25 +01003669 /* prefixed default case make sense only for the prefix of the schema itself */
3670 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3671 "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").",
3672 prefix_len, prefix);
3673 return LY_EVALID;
3674 }
3675 ch->dflt = (struct lysc_node_case*)lys_child(node, node->module, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE);
3676 if (!ch->dflt) {
3677 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3678 "Default case \"%s\" not found.", dflt);
3679 return LY_EVALID;
3680 }
3681 /* no mandatory nodes directly under the default case */
3682 LY_LIST_FOR(ch->dflt->child, iter) {
Radek Krejcife13da42019-02-15 14:51:01 +01003683 if (iter->parent != (struct lysc_node*)ch->dflt) {
3684 break;
3685 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003686 if (iter->flags & LYS_MAND_TRUE) {
3687 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3688 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt);
3689 return LY_EVALID;
3690 }
3691 }
Radek Krejci01342af2019-01-03 15:18:08 +01003692 ch->dflt->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01003693 return LY_SUCCESS;
3694}
3695
Radek Krejciccd20f12019-02-15 14:12:27 +01003696static LY_ERR
3697lys_compile_deviation_set_choice_dflt(struct lysc_ctx *ctx, const char *devnodeid, const char *dflt, struct lysc_node_choice *ch)
3698{
3699 struct lys_module *mod;
3700 const char *prefix = NULL, *name;
3701 size_t prefix_len = 0;
3702 struct lysc_node_case *cs;
3703 struct lysc_node *node;
3704
3705 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3706 name = strchr(dflt, ':');
3707 if (name) {
3708 prefix = dflt;
3709 prefix_len = name - prefix;
3710 ++name;
3711 } else {
3712 name = dflt;
3713 }
3714 /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */
3715 if (prefix) {
3716 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
3717 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3718 "Invalid deviation (%s) adding \"default\" property \"%s\" of choice. "
3719 "The prefix does not match any imported module of the deviation module.",
3720 devnodeid, dflt);
3721 return LY_EVALID;
3722 }
3723 } else {
3724 mod = ctx->mod;
3725 }
3726 /* get the default case */
3727 cs = (struct lysc_node_case*)lys_child((struct lysc_node*)ch, mod, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE);
3728 if (!cs) {
3729 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3730 "Invalid deviation (%s) adding \"default\" property \"%s\" of choice - the specified case does not exists.",
3731 devnodeid, dflt);
3732 return LY_EVALID;
3733 }
3734
3735 /* check that there is no mandatory node */
3736 LY_LIST_FOR(cs->child, node) {
Radek Krejcife13da42019-02-15 14:51:01 +01003737 if (node->parent != (struct lysc_node*)cs) {
3738 break;
3739 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003740 if (node->flags & LYS_MAND_TRUE) {
3741 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3742 "Invalid deviation (%s) adding \"default\" property \"%s\" of choice - "
3743 "mandatory node \"%s\" under the default case.", devnodeid, dflt, node->name);
3744 return LY_EVALID;
3745 }
3746 }
3747
3748 /* set the default case in choice */
3749 ch->dflt = cs;
3750 cs->flags |= LYS_SET_DFLT;
3751
3752 return LY_SUCCESS;
3753}
3754
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003755/**
Radek Krejci056d0a82018-12-06 16:57:25 +01003756 * @brief Compile parsed choice node information.
3757 * @param[in] ctx Compile context
3758 * @param[in] node_p Parsed choice node.
Radek Krejci056d0a82018-12-06 16:57:25 +01003759 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
Radek Krejci76b3e962018-12-14 17:01:25 +01003760 * is enriched with the choice-specific information.
Radek Krejci056d0a82018-12-06 16:57:25 +01003761 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3762 */
3763static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003764lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01003765{
3766 struct lysp_node_choice *ch_p = (struct lysp_node_choice*)node_p;
3767 struct lysc_node_choice *ch = (struct lysc_node_choice*)node;
3768 struct lysp_node *child_p, *case_child_p;
Radek Krejcia9026eb2018-12-12 16:04:47 +01003769 struct lys_module;
Radek Krejci056d0a82018-12-06 16:57:25 +01003770 LY_ERR ret = LY_SUCCESS;
3771
Radek Krejci056d0a82018-12-06 16:57:25 +01003772 LY_LIST_FOR(ch_p->child, child_p) {
3773 if (child_p->nodetype == LYS_CASE) {
3774 LY_LIST_FOR(((struct lysp_node_case*)child_p)->child, case_child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003775 LY_CHECK_RET(lys_compile_node(ctx, case_child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01003776 }
3777 } else {
Radek Krejciec4da802019-05-02 13:02:41 +02003778 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01003779 }
3780 }
3781
3782 /* default branch */
Radek Krejcia9026eb2018-12-12 16:04:47 +01003783 if (ch_p->dflt) {
Radek Krejci76b3e962018-12-14 17:01:25 +01003784 LY_CHECK_RET(lys_compile_node_choice_dflt(ctx, ch_p->dflt, ch));
Radek Krejcia9026eb2018-12-12 16:04:47 +01003785 }
Radek Krejci056d0a82018-12-06 16:57:25 +01003786
Radek Krejci9800fb82018-12-13 14:26:23 +01003787 return ret;
3788}
3789
3790/**
3791 * @brief Compile parsed anydata or anyxml node information.
3792 * @param[in] ctx Compile context
3793 * @param[in] node_p Parsed anydata or anyxml node.
Radek Krejci9800fb82018-12-13 14:26:23 +01003794 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3795 * is enriched with the any-specific information.
3796 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3797 */
3798static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003799lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9800fb82018-12-13 14:26:23 +01003800{
3801 struct lysp_node_anydata *any_p = (struct lysp_node_anydata*)node_p;
3802 struct lysc_node_anydata *any = (struct lysc_node_anydata*)node;
3803 unsigned int u;
3804 LY_ERR ret = LY_SUCCESS;
3805
Radek Krejciec4da802019-05-02 13:02:41 +02003806 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, u, lys_compile_must, ret, done);
Radek Krejci9800fb82018-12-13 14:26:23 +01003807
3808 if (any->flags & LYS_CONFIG_W) {
3809 LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended.",
3810 ly_stmt2str(any->nodetype == LYS_ANYDATA ? YANG_ANYDATA : YANG_ANYXML));
3811 }
Radek Krejci056d0a82018-12-06 16:57:25 +01003812done:
3813 return ret;
3814}
3815
Radek Krejcib56c7502019-02-13 14:19:54 +01003816/**
Radek Krejci056d0a82018-12-06 16:57:25 +01003817 * @brief Connect the node into the siblings list and check its name uniqueness.
3818 *
3819 * @param[in] ctx Compile context
3820 * @param[in] parent Parent node holding the children list, in case of node from a choice's case,
3821 * the choice itself is expected instead of a specific case node.
3822 * @param[in] node Schema node to connect into the list.
3823 * @return LY_ERR value - LY_SUCCESS or LY_EEXIST.
3824 */
3825static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003826lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01003827{
3828 struct lysc_node **children;
3829
3830 if (node->nodetype == LYS_CASE) {
3831 children = (struct lysc_node**)&((struct lysc_node_choice*)parent)->cases;
3832 } else {
Radek Krejciec4da802019-05-02 13:02:41 +02003833 children = lysc_node_children_p(parent, ctx->options);
Radek Krejci056d0a82018-12-06 16:57:25 +01003834 }
3835 if (children) {
3836 if (!(*children)) {
3837 /* first child */
3838 *children = node;
3839 } else if (*children != node) {
3840 /* by the condition in previous branch we cover the choice/case children
3841 * - the children list is shared by the choice and the the first case, in addition
3842 * the first child of each case must be referenced from the case node. So the node is
3843 * actually always already inserted in case it is the first children - so here such
3844 * a situation actually corresponds to the first branch */
3845 /* insert at the end of the parent's children list */
3846 (*children)->prev->next = node;
3847 node->prev = (*children)->prev;
3848 (*children)->prev = node;
3849
3850 /* check the name uniqueness */
3851 if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent),
3852 lysc_node_notifs(parent), node->name, node)) {
3853 return LY_EEXIST;
3854 }
3855 }
3856 }
3857 return LY_SUCCESS;
3858}
3859
Radek Krejci95710c92019-02-11 15:49:55 +01003860/**
Radek Krejcib56c7502019-02-13 14:19:54 +01003861 * @brief Get the XPath context node for the given schema node.
3862 * @param[in] start The schema node where the XPath expression appears.
3863 * @return The context node to evaluate XPath expression in given schema node.
3864 * @return NULL in case the context node is the root node.
3865 */
3866static struct lysc_node *
3867lysc_xpath_context(struct lysc_node *start)
3868{
3869 for (; start && !(start->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_ACTION | LYS_NOTIF));
3870 start = start->parent);
3871 return start;
3872}
3873
3874/**
3875 * @brief Prepare the case structure in choice node for the new data node.
3876 *
3877 * It is able to handle implicit as well as explicit cases and the situation when the case has multiple data nodes and the case was already
3878 * created in the choice when the first child was processed.
3879 *
3880 * @param[in] ctx Compile context.
Radek Krejci95710c92019-02-11 15:49:55 +01003881 * @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,
3882 * it is the LYS_CHOICE node or LYS_AUGMENT node.
Radek Krejcib56c7502019-02-13 14:19:54 +01003883 * @param[in] ch The compiled choice structure where the new case structures are created (if needed).
3884 * @param[in] child The new data node being part of a case (no matter if explicit or implicit).
3885 * @return The case structure where the child node belongs to, NULL in case of error. Note that the child is not connected into the siblings list,
3886 * it is linked from the case structure only in case it is its first child.
Radek Krejci95710c92019-02-11 15:49:55 +01003887 */
Radek Krejci056d0a82018-12-06 16:57:25 +01003888static struct lysc_node_case*
Radek Krejciec4da802019-05-02 13:02:41 +02003889lys_compile_node_case(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node_choice *ch, struct lysc_node *child)
Radek Krejci056d0a82018-12-06 16:57:25 +01003890{
3891 struct lysc_node *iter;
Radek Krejci98b1a662019-04-23 10:25:50 +02003892 struct lysc_node_case *cs = NULL;
Radek Krejci00b874b2019-02-12 10:54:50 +01003893 struct lysc_when **when;
Radek Krejci056d0a82018-12-06 16:57:25 +01003894 unsigned int u;
3895 LY_ERR ret;
3896
Radek Krejci95710c92019-02-11 15:49:55 +01003897#define UNIQUE_CHECK(NAME, MOD) \
Radek Krejci056d0a82018-12-06 16:57:25 +01003898 LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \
Radek Krejci95710c92019-02-11 15:49:55 +01003899 if (iter->module == MOD && !strcmp(iter->name, NAME)) { \
Radek Krejci056d0a82018-12-06 16:57:25 +01003900 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \
3901 return NULL; \
3902 } \
3903 }
3904
Radek Krejci95710c92019-02-11 15:49:55 +01003905 if (node_p->nodetype == LYS_CHOICE || node_p->nodetype == LYS_AUGMENT) {
3906 UNIQUE_CHECK(child->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01003907
3908 /* we have to add an implicit case node into the parent choice */
3909 cs = calloc(1, sizeof(struct lysc_node_case));
3910 DUP_STRING(ctx->ctx, child->name, cs->name);
3911 cs->flags = ch->flags & LYS_STATUS_MASK;
Radek Krejci95710c92019-02-11 15:49:55 +01003912 } else if (node_p->nodetype == LYS_CASE) {
Radek Krejci056d0a82018-12-06 16:57:25 +01003913 if (ch->cases && (node_p == ch->cases->prev->sp)) {
3914 /* the case is already present since the child is not its first children */
3915 return (struct lysc_node_case*)ch->cases->prev;
3916 }
Radek Krejci95710c92019-02-11 15:49:55 +01003917 UNIQUE_CHECK(node_p->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01003918
3919 /* explicit parent case is not present (this is its first child) */
3920 cs = calloc(1, sizeof(struct lysc_node_case));
3921 DUP_STRING(ctx->ctx, node_p->name, cs->name);
3922 cs->flags = LYS_STATUS_MASK & node_p->flags;
3923 cs->sp = node_p;
3924
Radek Krejcib1b59152019-01-07 13:21:56 +01003925 /* check the case's status (don't need to solve uses_status since case statement cannot be directly in grouping statement */
Radek Krejcibae745f2019-04-09 16:28:00 +02003926 LY_CHECK_GOTO(lys_compile_status(ctx, &cs->flags, ch->flags), error);
Radek Krejci00b874b2019-02-12 10:54:50 +01003927
3928 if (node_p->when) {
3929 LY_ARRAY_NEW_GOTO(ctx->ctx, cs->when, when, ret, error);
Radek Krejciec4da802019-05-02 13:02:41 +02003930 ret = lys_compile_when(ctx, node_p->when, when);
Radek Krejci00b874b2019-02-12 10:54:50 +01003931 LY_CHECK_GOTO(ret, error);
Radek Krejcib56c7502019-02-13 14:19:54 +01003932 (*when)->context = lysc_xpath_context(ch->parent);
Radek Krejci00b874b2019-02-12 10:54:50 +01003933 }
Radek Krejciec4da802019-05-02 13:02:41 +02003934 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, cs->iffeatures, u, lys_compile_iffeature, ret, error);
Radek Krejci95710c92019-02-11 15:49:55 +01003935 } else {
3936 LOGINT(ctx->ctx);
3937 goto error;
Radek Krejci056d0a82018-12-06 16:57:25 +01003938 }
3939 cs->module = ctx->mod;
3940 cs->prev = (struct lysc_node*)cs;
3941 cs->nodetype = LYS_CASE;
Radek Krejciec4da802019-05-02 13:02:41 +02003942 lys_compile_node_connect(ctx, (struct lysc_node*)ch, (struct lysc_node*)cs);
Radek Krejci056d0a82018-12-06 16:57:25 +01003943 cs->parent = (struct lysc_node*)ch;
3944 cs->child = child;
3945
3946 return cs;
3947error:
Radek Krejci1b1e9252019-04-24 08:45:50 +02003948 if (cs) {
3949 lysc_node_free(ctx->ctx, (struct lysc_node*)cs);
3950 }
Radek Krejci056d0a82018-12-06 16:57:25 +01003951 return NULL;
3952
3953#undef UNIQUE_CHECK
3954}
3955
Radek Krejcib56c7502019-02-13 14:19:54 +01003956/**
Radek Krejci93dcc392019-02-19 10:43:38 +01003957 * @brief Apply refined or deviated config to the target node.
Radek Krejcib56c7502019-02-13 14:19:54 +01003958 *
3959 * @param[in] ctx Compile context.
Radek Krejci93dcc392019-02-19 10:43:38 +01003960 * @param[in] node Target node where the config is supposed to be changed.
3961 * @param[in] config_flag Node's config flag to be applied to the @p node.
3962 * @param[in] nodeid Schema nodeid used to identify target of refine/deviation (for logging).
Radek Krejcib56c7502019-02-13 14:19:54 +01003963 * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is
3964 * done only on the node for which the refine was created. The function applies also recursively to apply the config change
Radek Krejci93dcc392019-02-19 10:43:38 +01003965 * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes.
3966 * @param[in] refine_flag Flag to distinguish if the change is caused by refine (flag set) or deviation (for logging).
Radek Krejcib56c7502019-02-13 14:19:54 +01003967 * @return LY_ERR value.
3968 */
Radek Krejci76b3e962018-12-14 17:01:25 +01003969static LY_ERR
Radek Krejci93dcc392019-02-19 10:43:38 +01003970lys_compile_change_config(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t config_flag,
3971 const char *nodeid, int inheriting, int refine_flag)
Radek Krejci76b3e962018-12-14 17:01:25 +01003972{
3973 struct lysc_node *child;
Radek Krejci93dcc392019-02-19 10:43:38 +01003974 uint16_t config = config_flag & LYS_CONFIG_MASK;
Radek Krejci76b3e962018-12-14 17:01:25 +01003975
3976 if (config == (node->flags & LYS_CONFIG_MASK)) {
3977 /* nothing to do */
3978 return LY_SUCCESS;
3979 }
3980
3981 if (!inheriting) {
Radek Krejci93dcc392019-02-19 10:43:38 +01003982 /* explicit change */
Radek Krejci76b3e962018-12-14 17:01:25 +01003983 if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) {
3984 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci93dcc392019-02-19 10:43:38 +01003985 "Invalid %s of config in \"%s\" - configuration node cannot be child of any state data node.",
3986 refine_flag ? "refine" : "deviation", nodeid);
Radek Krejci76b3e962018-12-14 17:01:25 +01003987 return LY_EVALID;
3988 }
Radek Krejci93dcc392019-02-19 10:43:38 +01003989 node->flags |= LYS_SET_CONFIG;
3990 } else {
3991 if (node->flags & LYS_SET_CONFIG) {
3992 if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) {
3993 /* setting config flags, but have node with explicit config true */
3994 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3995 "Invalid %s of config in \"%s\" - configuration node cannot be child of any state data node.",
3996 refine_flag ? "refine" : "deviation", nodeid);
3997 return LY_EVALID;
3998 }
3999 /* do not change config on nodes where the config is explicitely set, this does not apply to
4000 * nodes, which are being changed explicitly (targets of refine or deviation) */
4001 return LY_SUCCESS;
4002 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004003 }
4004 node->flags &= ~LYS_CONFIG_MASK;
4005 node->flags |= config;
4006
4007 /* inherit the change into the children */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004008 LY_LIST_FOR((struct lysc_node*)lysc_node_children(node, 0), child) {
Radek Krejci93dcc392019-02-19 10:43:38 +01004009 LY_CHECK_RET(lys_compile_change_config(ctx, child, config_flag, nodeid, 1, refine_flag));
Radek Krejci76b3e962018-12-14 17:01:25 +01004010 }
4011
Radek Krejci76b3e962018-12-14 17:01:25 +01004012 return LY_SUCCESS;
4013}
4014
Radek Krejcib56c7502019-02-13 14:19:54 +01004015/**
4016 * @brief Set LYS_MAND_TRUE flag for the non-presence container parents.
4017 *
4018 * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate
4019 * the flag to such parents from a mandatory children.
4020 *
4021 * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory.
4022 * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag
4023 * (mandatory children was removed).
4024 */
Radek Krejcife909632019-02-12 15:34:42 +01004025void
4026lys_compile_mandatory_parents(struct lysc_node *parent, int add)
4027{
4028 struct lysc_node *iter;
4029
4030 if (add) { /* set flag */
4031 for (; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
4032 parent = parent->parent) {
4033 parent->flags |= LYS_MAND_TRUE;
4034 }
4035 } else { /* unset flag */
4036 for (; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004037 for (iter = (struct lysc_node*)lysc_node_children(parent, 0); iter; iter = iter->next) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004038 if (iter->flags & LYS_MAND_TRUE) {
Radek Krejcife909632019-02-12 15:34:42 +01004039 /* there is another mandatory node */
4040 return;
4041 }
4042 }
4043 /* unset mandatory flag - there is no mandatory children in the non-presence container */
4044 parent->flags &= ~LYS_MAND_TRUE;
4045 }
4046 }
4047}
4048
Radek Krejci056d0a82018-12-06 16:57:25 +01004049/**
Radek Krejci3641f562019-02-13 15:38:40 +01004050 * @brief Internal sorting process for the lys_compile_augment_sort().
4051 * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result.
4052 * @param[in,out] result Sized array to store the sorted list of augments. The array is expected
4053 * to be allocated to hold the complete list, its size is just incremented by adding another item.
4054 */
4055static void
4056lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result)
4057{
4058 unsigned int v;
4059 size_t len;
4060
4061 len = strlen(aug_p->nodeid);
4062 LY_ARRAY_FOR(result, v) {
4063 if (strlen(result[v]->nodeid) <= len) {
4064 continue;
4065 }
4066 if (v < LY_ARRAY_SIZE(result)) {
4067 /* move the rest of array */
4068 memmove(&result[v + 1], &result[v], (LY_ARRAY_SIZE(result) - v) * sizeof *result);
4069 break;
4070 }
4071 }
4072 result[v] = aug_p;
4073 LY_ARRAY_INCREMENT(result);
4074}
4075
4076/**
4077 * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment).
4078 *
4079 * The sorting is based only on the length of the augment's path since it guarantee the correct order
4080 * (it doesn't matter the /a/x is done before /a/b/c from the example above).
4081 *
4082 * @param[in] ctx Compile context.
4083 * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken).
4084 * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's)
4085 * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules.
4086 * @param[out] augments Resulting sorted sized array of pointers to the augments.
4087 * @return LY_ERR value.
4088 */
4089LY_ERR
4090lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments)
4091{
4092 struct lysp_augment **result = NULL;
4093 unsigned int u, v;
4094 size_t count = 0;
4095
4096 assert(augments);
4097
4098 /* get count of the augments in module and all its submodules */
4099 if (aug_p) {
4100 count += LY_ARRAY_SIZE(aug_p);
4101 }
4102 LY_ARRAY_FOR(inc_p, u) {
4103 if (inc_p[u].submodule->augments) {
4104 count += LY_ARRAY_SIZE(inc_p[u].submodule->augments);
4105 }
4106 }
4107
4108 if (!count) {
4109 *augments = NULL;
4110 return LY_SUCCESS;
4111 }
4112 LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM);
4113
4114 /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them
4115 * together, so there can be even /z/y betwwen them. */
4116 LY_ARRAY_FOR(aug_p, u) {
4117 lys_compile_augment_sort_(&aug_p[u], result);
4118 }
4119 LY_ARRAY_FOR(inc_p, u) {
4120 LY_ARRAY_FOR(inc_p[u].submodule->augments, v) {
4121 lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result);
4122 }
4123 }
4124
4125 *augments = result;
4126 return LY_SUCCESS;
4127}
4128
4129/**
4130 * @brief Compile the parsed augment connecting it into its target.
4131 *
4132 * It is expected that all the data referenced in path are present - augments are ordered so that augment B
4133 * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path
4134 * are already implemented and compiled.
4135 *
4136 * @param[in] ctx Compile context.
4137 * @param[in] aug_p Parsed augment to compile.
Radek Krejci3641f562019-02-13 15:38:40 +01004138 * @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
4139 * children in case of the augmenting uses data.
4140 * @return LY_SUCCESS on success.
4141 * @return LY_EVALID on failure.
4142 */
4143LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004144lys_compile_augment(struct lysc_ctx *ctx, struct lysp_augment *aug_p, const struct lysc_node *parent)
Radek Krejci3641f562019-02-13 15:38:40 +01004145{
4146 LY_ERR ret = LY_SUCCESS;
4147 struct lysp_node *node_p, *case_node_p;
4148 struct lysc_node *target; /* target target of the augment */
4149 struct lysc_node *node;
Radek Krejci3641f562019-02-13 15:38:40 +01004150 struct lysc_when **when, *when_shared;
4151 int allow_mandatory = 0;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004152 uint16_t flags = 0;
4153 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02004154 int opt_prev = ctx->options;
Radek Krejci3641f562019-02-13 15:38:40 +01004155
Radek Krejci7af64242019-02-18 13:07:53 +01004156 ret = lys_resolve_schema_nodeid(ctx, aug_p->nodeid, 0, parent, parent ? parent->module : ctx->mod_def,
Radek Krejci3641f562019-02-13 15:38:40 +01004157 LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004158 1, (const struct lysc_node**)&target, &flags);
Radek Krejci3641f562019-02-13 15:38:40 +01004159 if (ret != LY_SUCCESS) {
4160 if (ret == LY_EDENIED) {
4161 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4162 "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.",
4163 parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype));
4164 }
4165 return LY_EVALID;
4166 }
4167
4168 /* check for mandatory nodes
4169 * - new cases augmenting some choice can have mandatory nodes
4170 * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement
4171 */
Radek Krejci733988a2019-02-15 15:12:44 +01004172 if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) {
Radek Krejci3641f562019-02-13 15:38:40 +01004173 allow_mandatory = 1;
4174 }
4175
4176 when_shared = NULL;
4177 LY_LIST_FOR(aug_p->child, node_p) {
4178 /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */
4179 if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE)
4180 && !((target->nodetype & (LYS_CONTAINER | LYS_LIST)) && (node_p->nodetype & (LYS_ACTION | LYS_NOTIF)))
Radek Krejci2d56a892019-02-19 09:05:26 +01004181 && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES)
4182 && !(node_p->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_CHOICE | LYS_LEAF | LYS_LIST | LYS_LEAFLIST))) {
Radek Krejci3641f562019-02-13 15:38:40 +01004183 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4184 "Invalid augment (%s) of %s node which is not allowed to contain %s node \"%s\".",
4185 aug_p->nodeid, lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name);
4186 return LY_EVALID;
4187 }
4188
4189 /* compile the children */
Radek Krejciec4da802019-05-02 13:02:41 +02004190 ctx->options |= flags;
Radek Krejci3641f562019-02-13 15:38:40 +01004191 if (node_p->nodetype != LYS_CASE) {
Radek Krejciec4da802019-05-02 13:02:41 +02004192 LY_CHECK_RET(lys_compile_node(ctx, node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004193 } else {
4194 LY_LIST_FOR(((struct lysp_node_case *)node_p)->child, case_node_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02004195 LY_CHECK_RET(lys_compile_node(ctx, case_node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004196 }
4197 }
Radek Krejciec4da802019-05-02 13:02:41 +02004198 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004199
Radek Krejcife13da42019-02-15 14:51:01 +01004200 /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children,
4201 * here we gets the last created node as last children of our parent */
Radek Krejci3641f562019-02-13 15:38:40 +01004202 if (target->nodetype == LYS_CASE) {
Radek Krejcife13da42019-02-15 14:51:01 +01004203 /* the compiled node is the last child of the target (but it is a case, so we have to be careful and stop) */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004204 for (node = (struct lysc_node*)lysc_node_children(target, flags); node->next && node->next->parent == node->parent; node = node->next);
Radek Krejci3641f562019-02-13 15:38:40 +01004205 } else if (target->nodetype == LYS_CHOICE) {
4206 /* to pass when statement, we need the last case no matter if it is explicit or implicit case */
4207 node = ((struct lysc_node_choice*)target)->cases->prev;
4208 } else {
4209 /* the compiled node is the last child of the target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004210 node = lysc_node_children(target, flags)->prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004211 }
4212
Radek Krejci733988a2019-02-15 15:12:44 +01004213 if (!allow_mandatory && (node->flags & LYS_CONFIG_W) && (node->flags & LYS_MAND_TRUE)) {
Radek Krejci3641f562019-02-13 15:38:40 +01004214 node->flags &= ~LYS_MAND_TRUE;
4215 lys_compile_mandatory_parents(target, 0);
4216 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4217 "Invalid augment (%s) adding mandatory node \"%s\" without making it conditional via when statement.",
4218 aug_p->nodeid, node->name);
4219 return LY_EVALID;
4220 }
4221
4222 /* pass augment's when to all the children */
4223 if (aug_p->when) {
4224 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
4225 if (!when_shared) {
Radek Krejciec4da802019-05-02 13:02:41 +02004226 ret = lys_compile_when(ctx, aug_p->when, when);
Radek Krejci3641f562019-02-13 15:38:40 +01004227 LY_CHECK_GOTO(ret, error);
4228 (*when)->context = lysc_xpath_context(target);
4229 when_shared = *when;
4230 } else {
4231 ++when_shared->refcount;
4232 (*when) = when_shared;
4233 }
4234 }
4235 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004236
Radek Krejciec4da802019-05-02 13:02:41 +02004237 ctx->options |= flags;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004238 switch (target->nodetype) {
4239 case LYS_CONTAINER:
Radek Krejci05b774b2019-02-25 13:26:18 +01004240 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_container*)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004241 u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004242 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_container*)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004243 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004244 break;
4245 case LYS_LIST:
Radek Krejci05b774b2019-02-25 13:26:18 +01004246 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_list*)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004247 u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004248 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_list*)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004249 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004250 break;
4251 default:
Radek Krejciec4da802019-05-02 13:02:41 +02004252 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004253 if (aug_p->actions) {
4254 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4255 "Invalid augment (%s) of %s node which is not allowed to contain RPC/action node \"%s\".",
4256 aug_p->nodeid, lys_nodetype2str(target->nodetype), aug_p->actions[0].name);
4257 return LY_EVALID;
4258 }
4259 if (aug_p->notifs) {
4260 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4261 "Invalid augment (%s) of %s node which is not allowed to contain Notification node \"%s\".",
4262 aug_p->nodeid, lys_nodetype2str(target->nodetype), aug_p->notifs[0].name);
4263 return LY_EVALID;
4264 }
4265 }
Radek Krejci3641f562019-02-13 15:38:40 +01004266
4267error:
Radek Krejciec4da802019-05-02 13:02:41 +02004268 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004269 return ret;
4270}
4271
4272/**
Radek Krejcif1421c22019-02-19 13:05:20 +01004273 * @brief Apply refined or deviated mandatory flag to the target node.
4274 *
4275 * @param[in] ctx Compile context.
4276 * @param[in] node Target node where the mandatory property is supposed to be changed.
4277 * @param[in] mandatory_flag Node's mandatory flag to be applied to the @p node.
4278 * @param[in] nodeid Schema nodeid used to identify target of refine/deviation (for logging).
4279 * @param[in] refine_flag Flag to distinguish if the change is caused by refine (flag set) or deviation (for logging).
Radek Krejci551b12c2019-02-19 16:11:21 +01004280 * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations,
4281 * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure,
4282 * the tests are skipped here, but they are supposed to be performed after all the deviations are applied.
Radek Krejcif1421c22019-02-19 13:05:20 +01004283 * @return LY_ERR value.
4284 */
4285static LY_ERR
4286lys_compile_change_mandatory(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t mandatory_flag, const char *nodeid, int refine_flag)
4287{
4288 if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) {
4289 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4290 "Invalid %s of mandatory in \"%s\" - %s cannot hold mandatory statement.",
4291 refine_flag ? "refine" : "deviation", nodeid, lys_nodetype2str(node->nodetype));
4292 return LY_EVALID;
4293 }
4294
4295 if (mandatory_flag & LYS_MAND_TRUE) {
4296 /* check if node has default value */
4297 if (node->nodetype & LYS_LEAF) {
4298 if (node->flags & LYS_SET_DFLT) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004299 if (refine_flag) {
4300 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4301 "Invalid refine of mandatory in \"%s\" - leaf already has \"default\" statement.", nodeid);
4302 return LY_EVALID;
4303 }
Radek Krejcif1421c22019-02-19 13:05:20 +01004304 } else {
4305 /* remove the default value taken from the leaf's type */
4306 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)node)->dflt);
4307 ((struct lysc_node_leaf*)node)->dflt = NULL;
4308 }
4309 } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004310 if (refine_flag) {
4311 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4312 "Invalid refine of mandatory in \"%s\" - choice already has \"default\" statement.", nodeid);
4313 return LY_EVALID;
4314 }
Radek Krejcif1421c22019-02-19 13:05:20 +01004315 }
Radek Krejci551b12c2019-02-19 16:11:21 +01004316 if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004317 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci551b12c2019-02-19 16:11:21 +01004318 "Invalid refine of mandatory in \"%s\" under the default case.", nodeid);
Radek Krejcif1421c22019-02-19 13:05:20 +01004319 return LY_EVALID;
4320 }
4321
4322 node->flags &= ~LYS_MAND_FALSE;
4323 node->flags |= LYS_MAND_TRUE;
4324 lys_compile_mandatory_parents(node->parent, 1);
4325 } else {
4326 /* make mandatory false */
4327 node->flags &= ~LYS_MAND_TRUE;
4328 node->flags |= LYS_MAND_FALSE;
4329 lys_compile_mandatory_parents(node->parent, 0);
4330 if ((node->nodetype & LYS_LEAF) && !((struct lysc_node_leaf*)node)->dflt) {
4331 /* get the type's default value if any */
4332 DUP_STRING(ctx->ctx, ((struct lysc_node_leaf*)node)->type->dflt, ((struct lysc_node_leaf*)node)->dflt);
4333 }
4334 }
4335 return LY_SUCCESS;
4336}
4337
4338/**
Radek Krejcie86bf772018-12-14 11:39:53 +01004339 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
4340 * If present, also apply uses's modificators.
4341 *
4342 * @param[in] ctx Compile context
4343 * @param[in] uses_p Parsed uses schema node.
Radek Krejcie86bf772018-12-14 11:39:53 +01004344 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
4345 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
4346 * the compile context.
4347 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4348 */
4349static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004350lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent)
Radek Krejcie86bf772018-12-14 11:39:53 +01004351{
4352 struct lysp_node *node_p;
Radek Krejci01342af2019-01-03 15:18:08 +01004353 struct lysc_node *node, *child;
Radek Krejci12fb9142019-01-08 09:45:30 +01004354 /* context_node_fake allows us to temporarily isolate the nodes inserted from the grouping instead of uses */
Radek Krejci01342af2019-01-03 15:18:08 +01004355 struct lysc_node_container context_node_fake =
4356 {.nodetype = LYS_CONTAINER,
4357 .module = ctx->mod,
4358 .flags = parent ? parent->flags : 0,
4359 .child = NULL, .next = NULL,
Radek Krejcifc11bd72019-04-11 16:00:05 +02004360 .prev = (struct lysc_node*)&context_node_fake,
4361 .actions = NULL, .notifs = NULL};
Radek Krejciec4da802019-05-02 13:02:41 +02004362 struct lysp_grp *grp = NULL;
Radek Krejci76b3e962018-12-14 17:01:25 +01004363 unsigned int u, v, grp_stack_count;
Radek Krejcie86bf772018-12-14 11:39:53 +01004364 int found;
4365 const char *id, *name, *prefix;
4366 size_t prefix_len, name_len;
4367 struct lys_module *mod, *mod_old;
Radek Krejci76b3e962018-12-14 17:01:25 +01004368 struct lysp_refine *rfn;
Radek Krejcie86bf772018-12-14 11:39:53 +01004369 LY_ERR ret = LY_EVALID;
Radek Krejcif2271f12019-01-07 16:42:23 +01004370 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004371 uint16_t flags;
Radek Krejcif2271f12019-01-07 16:42:23 +01004372 struct ly_set refined = {0};
Radek Krejci00b874b2019-02-12 10:54:50 +01004373 struct lysc_when **when, *when_shared;
Radek Krejci3641f562019-02-13 15:38:40 +01004374 struct lysp_augment **augments = NULL;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004375 unsigned int actions_index, notifs_index;
4376 struct lysc_notif **notifs = NULL;
4377 struct lysc_action **actions = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01004378
4379 /* search for the grouping definition */
4380 found = 0;
4381 id = uses_p->name;
Radek Krejci15e99fc2019-04-08 14:29:39 +02004382 LY_CHECK_RET(lys_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID);
Radek Krejcie86bf772018-12-14 11:39:53 +01004383 if (prefix) {
4384 mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len);
4385 if (!mod) {
4386 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4387 "Invalid prefix used for grouping reference (%s).", uses_p->name);
4388 return LY_EVALID;
4389 }
4390 } else {
4391 mod = ctx->mod_def;
4392 }
4393 if (mod == ctx->mod_def) {
4394 for (node_p = uses_p->parent; !found && node_p; node_p = node_p->parent) {
Radek Krejciec4da802019-05-02 13:02:41 +02004395 grp = (struct lysp_grp*)lysp_node_groupings(node_p);
Radek Krejcie86bf772018-12-14 11:39:53 +01004396 LY_ARRAY_FOR(grp, u) {
4397 if (!strcmp(grp[u].name, name)) {
4398 grp = &grp[u];
4399 found = 1;
4400 break;
4401 }
4402 }
4403 }
4404 }
4405 if (!found) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004406 /* search in top-level groupings of the main module ... */
Radek Krejcie86bf772018-12-14 11:39:53 +01004407 grp = mod->parsed->groupings;
Radek Krejci76b3e962018-12-14 17:01:25 +01004408 if (grp) {
4409 for (u = 0; !found && u < LY_ARRAY_SIZE(grp); ++u) {
4410 if (!strcmp(grp[u].name, name)) {
4411 grp = &grp[u];
4412 found = 1;
4413 }
4414 }
4415 }
4416 if (!found && mod->parsed->includes) {
4417 /* ... and all the submodules */
4418 for (u = 0; !found && u < LY_ARRAY_SIZE(mod->parsed->includes); ++u) {
4419 grp = mod->parsed->includes[u].submodule->groupings;
4420 if (grp) {
4421 for (v = 0; !found && v < LY_ARRAY_SIZE(grp); ++v) {
4422 if (!strcmp(grp[v].name, name)) {
4423 grp = &grp[v];
4424 found = 1;
4425 }
4426 }
4427 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004428 }
4429 }
4430 }
4431 if (!found) {
4432 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4433 "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
4434 return LY_EVALID;
4435 }
4436
4437 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
4438 grp_stack_count = ctx->groupings.count;
4439 ly_set_add(&ctx->groupings, (void*)grp, 0);
4440 if (grp_stack_count == ctx->groupings.count) {
4441 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
4442 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4443 "Grouping \"%s\" references itself through a uses statement.", grp->name);
4444 return LY_EVALID;
4445 }
Radek Krejcif2de0ed2019-05-02 14:13:18 +02004446 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4447 /* remember that the grouping is instantiated to avoid its standalone validation */
4448 grp->flags |= LYS_USED_GRP;
4449 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004450
4451 /* switch context's mod_def */
4452 mod_old = ctx->mod_def;
4453 ctx->mod_def = mod;
4454
4455 /* check status */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004456 LY_CHECK_GOTO(lysc_check_status(ctx, uses_p->flags, mod_old, uses_p->name, grp->flags, mod, grp->name), cleanup);
Radek Krejcie86bf772018-12-14 11:39:53 +01004457
Radek Krejcifc11bd72019-04-11 16:00:05 +02004458 /* compile data nodes */
Radek Krejcie86bf772018-12-14 11:39:53 +01004459 LY_LIST_FOR(grp->data, node_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01004460 /* 0x3 in uses_status is a special bits combination to be able to detect status flags from uses */
Radek Krejciec4da802019-05-02 13:02:41 +02004461 LY_CHECK_GOTO(lys_compile_node(ctx, node_p, parent, (uses_p->flags & LYS_STATUS_MASK) | 0x3), cleanup);
4462 child = parent ? lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK)->prev : ctx->mod->compiled->data->prev;
Radek Krejci00b874b2019-02-12 10:54:50 +01004463
4464 /* some preparation for applying refines */
4465 if (grp->data == node_p) {
4466 /* remember the first child */
4467 context_node_fake.child = child;
Radek Krejci01342af2019-01-03 15:18:08 +01004468 }
4469 }
Radek Krejci00b874b2019-02-12 10:54:50 +01004470 when_shared = NULL;
Radek Krejci01342af2019-01-03 15:18:08 +01004471 LY_LIST_FOR(context_node_fake.child, child) {
4472 child->parent = (struct lysc_node*)&context_node_fake;
Radek Krejci00b874b2019-02-12 10:54:50 +01004473
Radek Krejcifc11bd72019-04-11 16:00:05 +02004474 /* pass uses's when to all the data children, actions and notifications are ignored */
Radek Krejci00b874b2019-02-12 10:54:50 +01004475 if (uses_p->when) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004476 LY_ARRAY_NEW_GOTO(ctx->ctx, child->when, when, ret, cleanup);
Radek Krejci00b874b2019-02-12 10:54:50 +01004477 if (!when_shared) {
Radek Krejciec4da802019-05-02 13:02:41 +02004478 LY_CHECK_GOTO(lys_compile_when(ctx, uses_p->when, when), cleanup);
Radek Krejcib56c7502019-02-13 14:19:54 +01004479 (*when)->context = lysc_xpath_context(parent);
Radek Krejci00b874b2019-02-12 10:54:50 +01004480 when_shared = *when;
4481 } else {
4482 ++when_shared->refcount;
4483 (*when) = when_shared;
4484 }
4485 }
Radek Krejci01342af2019-01-03 15:18:08 +01004486 }
4487 if (context_node_fake.child) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004488 /* child is the last data node added by grouping */
Radek Krejci01342af2019-01-03 15:18:08 +01004489 child = context_node_fake.child->prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004490 /* fix child link of our fake container to point to the first child of the original list */
Radek Krejciec4da802019-05-02 13:02:41 +02004491 context_node_fake.child->prev = parent ? lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK)->prev : ctx->mod->compiled->data->prev;
Radek Krejci76b3e962018-12-14 17:01:25 +01004492 }
4493
Radek Krejcifc11bd72019-04-11 16:00:05 +02004494 /* compile actions */
4495 actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs;
4496 if (actions) {
4497 actions_index = *actions ? LY_ARRAY_SIZE(*actions) : 0;
Radek Krejciec4da802019-05-02 13:02:41 +02004498 COMPILE_ARRAY1_GOTO(ctx, grp->actions, *actions, parent, u, lys_compile_action, 0, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004499 if (*actions && (uses_p->augments || uses_p->refines)) {
4500 /* but for augment and refine, we need to separate the compiled grouping's actions to avoid modification of others */
4501 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_SIZE(*actions) - actions_index, ret, cleanup);
4502 LY_ARRAY_SIZE(context_node_fake.actions) = LY_ARRAY_SIZE(*actions) - actions_index;
4503 memcpy(context_node_fake.actions, &(*actions)[actions_index], LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions);
4504 }
4505 }
4506
4507 /* compile notifications */
4508 notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs;
4509 if (notifs) {
4510 notifs_index = *notifs ? LY_ARRAY_SIZE(*notifs) : 0;
Radek Krejciec4da802019-05-02 13:02:41 +02004511 COMPILE_ARRAY1_GOTO(ctx, grp->notifs, *notifs, parent, u, lys_compile_notif, 0, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004512 if (*notifs && (uses_p->augments || uses_p->refines)) {
4513 /* but for augment and refine, we need to separate the compiled grouping's notification to avoid modification of others */
4514 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_SIZE(*notifs) - notifs_index, ret, cleanup);
4515 LY_ARRAY_SIZE(context_node_fake.notifs) = LY_ARRAY_SIZE(*notifs) - notifs_index;
4516 memcpy(context_node_fake.notifs, &(*notifs)[notifs_index], LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs);
4517 }
4518 }
4519
4520
Radek Krejci3641f562019-02-13 15:38:40 +01004521 /* sort and apply augments */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004522 LY_CHECK_GOTO(lys_compile_augment_sort(ctx, uses_p->augments, NULL, &augments), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004523 LY_ARRAY_FOR(augments, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02004524 LY_CHECK_GOTO(lys_compile_augment(ctx, augments[u], (struct lysc_node*)&context_node_fake), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004525 }
Radek Krejci12fb9142019-01-08 09:45:30 +01004526
Radek Krejcif0089082019-01-07 16:42:01 +01004527 /* reload previous context's mod_def */
4528 ctx->mod_def = mod_old;
4529
Radek Krejci76b3e962018-12-14 17:01:25 +01004530 /* apply refine */
4531 LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) {
Radek Krejci7af64242019-02-18 13:07:53 +01004532 LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, rfn->nodeid, 0, (struct lysc_node*)&context_node_fake, ctx->mod,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004533 0, 0, (const struct lysc_node**)&node, &flags),
Radek Krejcifc11bd72019-04-11 16:00:05 +02004534 cleanup);
Radek Krejcif2271f12019-01-07 16:42:23 +01004535 ly_set_add(&refined, node, LY_SET_OPT_USEASLIST);
Radek Krejci76b3e962018-12-14 17:01:25 +01004536
4537 /* default value */
4538 if (rfn->dflts) {
Radek Krejci01342af2019-01-03 15:18:08 +01004539 if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_SIZE(rfn->dflts) > 1) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004540 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4541 "Invalid refine of default in \"%s\" - %s cannot hold %d default values.",
4542 rfn->nodeid, lys_nodetype2str(node->nodetype), LY_ARRAY_SIZE(rfn->dflts));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004543 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004544 }
4545 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) {
4546 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4547 "Invalid refine of default in \"%s\" - %s cannot hold default value(s).",
4548 rfn->nodeid, lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004549 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004550 }
4551 if (node->nodetype == LYS_LEAF) {
4552 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)node)->dflt);
4553 DUP_STRING(ctx->ctx, rfn->dflts[0], ((struct lysc_node_leaf*)node)->dflt);
Radek Krejci01342af2019-01-03 15:18:08 +01004554 node->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004555 /* TODO check the default value according to type */
4556 } else if (node->nodetype == LYS_LEAFLIST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004557 if (ctx->mod->version < 2) {
Radek Krejci01342af2019-01-03 15:18:08 +01004558 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4559 "Invalid refine of default in leaf-list - the default statement is allowed only in YANG 1.1 modules.");
Radek Krejcifc11bd72019-04-11 16:00:05 +02004560 goto cleanup;
Radek Krejci01342af2019-01-03 15:18:08 +01004561 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004562 LY_ARRAY_FOR(((struct lysc_node_leaflist*)node)->dflts, u) {
4563 lydict_remove(ctx->ctx, ((struct lysc_node_leaflist*)node)->dflts[u]);
4564 }
4565 LY_ARRAY_FREE(((struct lysc_node_leaflist*)node)->dflts);
Radek Krejci01342af2019-01-03 15:18:08 +01004566 ((struct lysc_node_leaflist*)node)->dflts = NULL;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004567 LY_ARRAY_CREATE_GOTO(ctx->ctx, ((struct lysc_node_leaflist*)node)->dflts, LY_ARRAY_SIZE(rfn->dflts), ret, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004568 LY_ARRAY_FOR(rfn->dflts, u) {
4569 LY_ARRAY_INCREMENT(((struct lysc_node_leaflist*)node)->dflts);
4570 DUP_STRING(ctx->ctx, rfn->dflts[u], ((struct lysc_node_leaflist*)node)->dflts[u]);
4571 }
4572 /* TODO check the default values according to type */
4573 } else if (node->nodetype == LYS_CHOICE) {
Radek Krejci01342af2019-01-03 15:18:08 +01004574 if (((struct lysc_node_choice*)node)->dflt) {
4575 /* unset LYS_SET_DFLT from the current default case */
4576 ((struct lysc_node_choice*)node)->dflt->flags &= ~LYS_SET_DFLT;
4577 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02004578 LY_CHECK_GOTO(lys_compile_node_choice_dflt(ctx, rfn->dflts[0], (struct lysc_node_choice*)node), cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004579 }
4580 }
4581
Radek Krejci12fb9142019-01-08 09:45:30 +01004582 /* description */
4583 if (rfn->dsc) {
4584 FREE_STRING(ctx->ctx, node->dsc);
4585 node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0);
4586 }
4587
4588 /* reference */
4589 if (rfn->ref) {
4590 FREE_STRING(ctx->ctx, node->ref);
4591 node->ref = lydict_insert(ctx->ctx, rfn->ref, 0);
4592 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004593
4594 /* config */
4595 if (rfn->flags & LYS_CONFIG_MASK) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004596 if (!flags) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004597 LY_CHECK_GOTO(lys_compile_change_config(ctx, node, rfn->flags, rfn->nodeid, 0, 1), cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004598 } else {
4599 LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).",
4600 flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action", ctx->path);
4601 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004602 }
4603
4604 /* mandatory */
4605 if (rfn->flags & LYS_MAND_MASK) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004606 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, node, rfn->flags, rfn->nodeid, 1), cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004607 }
Radek Krejci9a54f1f2019-01-07 13:47:55 +01004608
4609 /* presence */
4610 if (rfn->presence) {
4611 if (node->nodetype != LYS_CONTAINER) {
4612 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4613 "Invalid refine of presence statement in \"%s\" - %s cannot hold the presence statement.",
4614 rfn->nodeid, lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004615 goto cleanup;
Radek Krejci9a54f1f2019-01-07 13:47:55 +01004616 }
4617 node->flags |= LYS_PRESENCE;
4618 }
Radek Krejci9a564c92019-01-07 14:53:57 +01004619
4620 /* must */
4621 if (rfn->musts) {
4622 switch (node->nodetype) {
4623 case LYS_LEAF:
Radek Krejciec4da802019-05-02 13:02:41 +02004624 COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_leaf*)node)->musts, u, lys_compile_must, ret, cleanup);
Radek Krejci9a564c92019-01-07 14:53:57 +01004625 break;
4626 case LYS_LEAFLIST:
Radek Krejciec4da802019-05-02 13:02:41 +02004627 COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_leaflist*)node)->musts, u, lys_compile_must, ret, cleanup);
Radek Krejci9a564c92019-01-07 14:53:57 +01004628 break;
4629 case LYS_LIST:
Radek Krejciec4da802019-05-02 13:02:41 +02004630 COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_list*)node)->musts, u, lys_compile_must, ret, cleanup);
Radek Krejci9a564c92019-01-07 14:53:57 +01004631 break;
4632 case LYS_CONTAINER:
Radek Krejciec4da802019-05-02 13:02:41 +02004633 COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_container*)node)->musts, u, lys_compile_must, ret, cleanup);
Radek Krejci9a564c92019-01-07 14:53:57 +01004634 break;
4635 case LYS_ANYXML:
4636 case LYS_ANYDATA:
Radek Krejciec4da802019-05-02 13:02:41 +02004637 COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_anydata*)node)->musts, u, lys_compile_must, ret, cleanup);
Radek Krejci9a564c92019-01-07 14:53:57 +01004638 break;
4639 default:
4640 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4641 "Invalid refine of must statement in \"%s\" - %s cannot hold any must statement.",
4642 rfn->nodeid, lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004643 goto cleanup;
Radek Krejci9a564c92019-01-07 14:53:57 +01004644 }
4645 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004646
4647 /* min/max-elements */
4648 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
4649 switch (node->nodetype) {
4650 case LYS_LEAFLIST:
4651 if (rfn->flags & LYS_SET_MAX) {
4652 ((struct lysc_node_leaflist*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
4653 }
4654 if (rfn->flags & LYS_SET_MIN) {
4655 ((struct lysc_node_leaflist*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01004656 if (rfn->min) {
4657 node->flags |= LYS_MAND_TRUE;
4658 lys_compile_mandatory_parents(node->parent, 1);
4659 } else {
4660 node->flags &= ~LYS_MAND_TRUE;
4661 lys_compile_mandatory_parents(node->parent, 0);
4662 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004663 }
4664 break;
4665 case LYS_LIST:
4666 if (rfn->flags & LYS_SET_MAX) {
4667 ((struct lysc_node_list*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
4668 }
4669 if (rfn->flags & LYS_SET_MIN) {
4670 ((struct lysc_node_list*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01004671 if (rfn->min) {
4672 node->flags |= LYS_MAND_TRUE;
4673 lys_compile_mandatory_parents(node->parent, 1);
4674 } else {
4675 node->flags &= ~LYS_MAND_TRUE;
4676 lys_compile_mandatory_parents(node->parent, 0);
4677 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004678 }
4679 break;
4680 default:
4681 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4682 "Invalid refine of %s statement in \"%s\" - %s cannot hold this statement.",
4683 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", rfn->nodeid, lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004684 goto cleanup;
Radek Krejci6b22ab72019-01-07 15:39:20 +01004685 }
4686 }
Radek Krejcif0089082019-01-07 16:42:01 +01004687
4688 /* if-feature */
4689 if (rfn->iffeatures) {
4690 /* any node in compiled tree can get additional if-feature, so do not check nodetype */
Radek Krejciec4da802019-05-02 13:02:41 +02004691 COMPILE_ARRAY_GOTO(ctx, rfn->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcif0089082019-01-07 16:42:01 +01004692 }
Radek Krejci01342af2019-01-03 15:18:08 +01004693 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004694
Radek Krejcif2271f12019-01-07 16:42:23 +01004695 /* do some additional checks of the changed nodes when all the refines are applied */
4696 for (u = 0; u < refined.count; ++u) {
4697 node = (struct lysc_node*)refined.objs[u];
4698 rfn = &uses_p->refines[u];
4699
4700 /* check possible conflict with default value (default added, mandatory left true) */
4701 if ((node->flags & LYS_MAND_TRUE) &&
4702 (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) ||
4703 ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) {
4704 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4705 "Invalid refine of default in \"%s\" - the node is mandatory.", rfn->nodeid);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004706 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01004707 }
4708
4709 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
4710 if (node->nodetype == LYS_LIST) {
4711 min = ((struct lysc_node_list*)node)->min;
4712 max = ((struct lysc_node_list*)node)->max;
4713 } else {
4714 min = ((struct lysc_node_leaflist*)node)->min;
4715 max = ((struct lysc_node_leaflist*)node)->max;
4716 }
4717 if (min > max) {
4718 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4719 "Invalid refine of %s statement in \"%s\" - \"min-elements\" is bigger than \"max-elements\".",
4720 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", rfn->nodeid);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004721 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01004722 }
4723 }
4724 }
4725
Radek Krejcie86bf772018-12-14 11:39:53 +01004726 ret = LY_SUCCESS;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004727
4728cleanup:
4729 /* fix connection of the children nodes from fake context node back into the parent */
4730 if (context_node_fake.child) {
4731 context_node_fake.child->prev = child;
4732 }
4733 LY_LIST_FOR(context_node_fake.child, child) {
4734 child->parent = parent;
4735 }
4736
4737 if (uses_p->augments || uses_p->refines) {
4738 /* return back actions and notifications in case they were separated for augment/refine processing */
Radek Krejci65e20e22019-04-12 09:44:37 +02004739 if (context_node_fake.actions) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004740 memcpy(&(*actions)[actions_index], context_node_fake.actions, LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions);
4741 LY_ARRAY_FREE(context_node_fake.actions);
4742 }
Radek Krejci65e20e22019-04-12 09:44:37 +02004743 if (context_node_fake.notifs) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004744 memcpy(&(*notifs)[notifs_index], context_node_fake.notifs, LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs);
4745 LY_ARRAY_FREE(context_node_fake.notifs);
4746 }
4747 }
4748
Radek Krejcie86bf772018-12-14 11:39:53 +01004749 /* reload previous context's mod_def */
4750 ctx->mod_def = mod_old;
4751 /* remove the grouping from the stack for circular groupings dependency check */
4752 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
4753 assert(ctx->groupings.count == grp_stack_count);
Radek Krejcif2271f12019-01-07 16:42:23 +01004754 ly_set_erase(&refined, NULL);
Radek Krejci3641f562019-02-13 15:38:40 +01004755 LY_ARRAY_FREE(augments);
Radek Krejcie86bf772018-12-14 11:39:53 +01004756
4757 return ret;
4758}
4759
Radek Krejcif2de0ed2019-05-02 14:13:18 +02004760/**
4761 * @brief Validate groupings that were defined but not directly used in the schema itself.
4762 *
4763 * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately),
4764 * but to have the complete result of the schema validity, even such groupings are supposed to be checked.
4765 */
4766static LY_ERR
4767lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysp_grp *grp)
4768{
4769 LY_ERR ret;
4770 struct lysp_node_uses fake_uses = {
4771 .parent = node_p,
4772 .nodetype = LYS_USES,
4773 .flags = 0, .next = NULL,
4774 .name = grp->name,
4775 .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL,
4776 .refines = NULL, .augments = NULL
4777 };
4778 struct lysc_node_container fake_container = {
4779 .nodetype = LYS_CONTAINER,
4780 .flags = node_p ? (node_p->flags & LYS_FLAGS_COMPILED_MASK) : 0,
4781 .module = ctx->mod,
4782 .sp = NULL, .parent = NULL, .next = NULL,
4783 .prev = (struct lysc_node*)&fake_container,
4784 .name = "fake",
4785 .dsc = NULL, .ref = NULL, .exts = NULL, .iffeatures = NULL, .when = NULL,
4786 .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL
4787 };
4788
4789 if (grp->parent) {
4790 LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name);
4791 }
4792 ret = lys_compile_uses(ctx, &fake_uses, (struct lysc_node*)&fake_container);
4793
4794 /* cleanup */
4795 lysc_node_container_free(ctx->ctx, &fake_container);
4796
4797 return ret;
4798}
Radek Krejcife909632019-02-12 15:34:42 +01004799
Radek Krejcie86bf772018-12-14 11:39:53 +01004800/**
Radek Krejcia3045382018-11-22 14:30:31 +01004801 * @brief Compile parsed schema node information.
4802 * @param[in] ctx Compile context
4803 * @param[in] node_p Parsed schema node.
Radek Krejcia3045382018-11-22 14:30:31 +01004804 * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is
4805 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
4806 * the compile context.
Radek Krejcib1b59152019-01-07 13:21:56 +01004807 * @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).
4808 * Zero means no uses, non-zero value with no status bit set mean the default status.
Radek Krejcia3045382018-11-22 14:30:31 +01004809 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4810 */
Radek Krejci19a96102018-11-15 13:38:09 +01004811static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004812lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *parent, uint16_t uses_status)
Radek Krejci19a96102018-11-15 13:38:09 +01004813{
4814 LY_ERR ret = LY_EVALID;
Radek Krejci056d0a82018-12-06 16:57:25 +01004815 struct lysc_node *node;
4816 struct lysc_node_case *cs;
Radek Krejci00b874b2019-02-12 10:54:50 +01004817 struct lysc_when **when;
Radek Krejci19a96102018-11-15 13:38:09 +01004818 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02004819 LY_ERR (*node_compile_spec)(struct lysc_ctx*, struct lysp_node*, struct lysc_node*);
Radek Krejci19a96102018-11-15 13:38:09 +01004820
4821 switch (node_p->nodetype) {
4822 case LYS_CONTAINER:
4823 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container));
4824 node_compile_spec = lys_compile_node_container;
4825 break;
4826 case LYS_LEAF:
4827 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf));
4828 node_compile_spec = lys_compile_node_leaf;
4829 break;
4830 case LYS_LIST:
4831 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004832 node_compile_spec = lys_compile_node_list;
Radek Krejci19a96102018-11-15 13:38:09 +01004833 break;
4834 case LYS_LEAFLIST:
4835 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist));
Radek Krejci0e5d8382018-11-28 16:37:53 +01004836 node_compile_spec = lys_compile_node_leaflist;
Radek Krejci19a96102018-11-15 13:38:09 +01004837 break;
Radek Krejci19a96102018-11-15 13:38:09 +01004838 case LYS_CHOICE:
4839 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice));
Radek Krejci056d0a82018-12-06 16:57:25 +01004840 node_compile_spec = lys_compile_node_choice;
Radek Krejci19a96102018-11-15 13:38:09 +01004841 break;
Radek Krejci19a96102018-11-15 13:38:09 +01004842 case LYS_ANYXML:
4843 case LYS_ANYDATA:
4844 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata));
Radek Krejci9800fb82018-12-13 14:26:23 +01004845 node_compile_spec = lys_compile_node_any;
Radek Krejci19a96102018-11-15 13:38:09 +01004846 break;
Radek Krejcie86bf772018-12-14 11:39:53 +01004847 case LYS_USES:
Radek Krejciec4da802019-05-02 13:02:41 +02004848 return lys_compile_uses(ctx, (struct lysp_node_uses*)node_p, parent);
Radek Krejci19a96102018-11-15 13:38:09 +01004849 default:
4850 LOGINT(ctx->ctx);
4851 return LY_EINT;
4852 }
4853 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
4854 node->nodetype = node_p->nodetype;
4855 node->module = ctx->mod;
4856 node->prev = node;
Radek Krejci0e5d8382018-11-28 16:37:53 +01004857 node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01004858
4859 /* config */
Radek Krejciec4da802019-05-02 13:02:41 +02004860 if (ctx->options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004861 /* ignore config statements inside RPC/action data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004862 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejciec4da802019-05-02 13:02:41 +02004863 node->flags |= (ctx->options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R;
4864 } else if (ctx->options & LYSC_OPT_NOTIFICATION) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004865 /* ignore config statements inside Notification data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004866 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004867 node->flags |= LYS_CONFIG_R;
4868 } else if (!(node->flags & LYS_CONFIG_MASK)) {
Radek Krejci19a96102018-11-15 13:38:09 +01004869 /* config not explicitely set, inherit it from parent */
4870 if (parent) {
4871 node->flags |= parent->flags & LYS_CONFIG_MASK;
4872 } else {
4873 /* default is config true */
4874 node->flags |= LYS_CONFIG_W;
4875 }
Radek Krejci93dcc392019-02-19 10:43:38 +01004876 } else {
4877 /* config set explicitely */
4878 node->flags |= LYS_SET_CONFIG;
Radek Krejci19a96102018-11-15 13:38:09 +01004879 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004880 if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
4881 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4882 "Configuration node cannot be child of any state data node.");
4883 goto error;
4884 }
Radek Krejci19a96102018-11-15 13:38:09 +01004885
Radek Krejcia6d57732018-11-29 13:40:37 +01004886 /* *list ordering */
4887 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
4888 if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004889 LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing %s (%s).",
Radek Krejciec4da802019-05-02 13:02:41 +02004890 (ctx->options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" :
4891 (ctx->options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path);
Radek Krejcia6d57732018-11-29 13:40:37 +01004892 node->flags &= ~LYS_ORDBY_MASK;
4893 node->flags |= LYS_ORDBY_SYSTEM;
4894 } else if (!(node->flags & LYS_ORDBY_MASK)) {
4895 /* default ordering is system */
4896 node->flags |= LYS_ORDBY_SYSTEM;
4897 }
4898 }
4899
Radek Krejci19a96102018-11-15 13:38:09 +01004900 /* status - it is not inherited by specification, but it does not make sense to have
4901 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Radek Krejci056d0a82018-12-06 16:57:25 +01004902 if (!parent || parent->nodetype != LYS_CHOICE) {
4903 /* in case of choice/case's children, postpone the check to the moment we know if
4904 * the parent is choice (parent here) or some case (so we have to get its flags to check) */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004905 LY_CHECK_GOTO(lys_compile_status(ctx, &node->flags, uses_status ? uses_status : (parent ? parent->flags : 0)), error);
Radek Krejci19a96102018-11-15 13:38:09 +01004906 }
4907
Radek Krejciec4da802019-05-02 13:02:41 +02004908 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci19a96102018-11-15 13:38:09 +01004909 node->sp = node_p;
4910 }
4911 DUP_STRING(ctx->ctx, node_p->name, node->name);
Radek Krejci12fb9142019-01-08 09:45:30 +01004912 DUP_STRING(ctx->ctx, node_p->dsc, node->dsc);
4913 DUP_STRING(ctx->ctx, node_p->ref, node->ref);
Radek Krejci00b874b2019-02-12 10:54:50 +01004914 if (node_p->when) {
4915 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
Radek Krejciec4da802019-05-02 13:02:41 +02004916 ret = lys_compile_when(ctx, node_p->when, when);
Radek Krejci00b874b2019-02-12 10:54:50 +01004917 LY_CHECK_GOTO(ret, error);
Radek Krejcib56c7502019-02-13 14:19:54 +01004918 (*when)->context = lysc_xpath_context(node);
Radek Krejci00b874b2019-02-12 10:54:50 +01004919 }
Radek Krejciec4da802019-05-02 13:02:41 +02004920 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, error);
4921 COMPILE_ARRAY_GOTO(ctx, node_p->exts, node->exts, u, lys_compile_ext, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01004922
4923 /* nodetype-specific part */
Radek Krejciec4da802019-05-02 13:02:41 +02004924 LY_CHECK_GOTO(node_compile_spec(ctx, node_p, node), error);
Radek Krejci19a96102018-11-15 13:38:09 +01004925
Radek Krejcife909632019-02-12 15:34:42 +01004926 /* inherit LYS_MAND_TRUE in parent containers */
4927 if (node->flags & LYS_MAND_TRUE) {
4928 lys_compile_mandatory_parents(parent, 1);
4929 }
4930
Radek Krejci19a96102018-11-15 13:38:09 +01004931 /* insert into parent's children */
Radek Krejcia3045382018-11-22 14:30:31 +01004932 if (parent) {
4933 if (parent->nodetype == LYS_CHOICE) {
Radek Krejciec4da802019-05-02 13:02:41 +02004934 cs = lys_compile_node_case(ctx, node_p->parent, (struct lysc_node_choice*)parent, node);
Radek Krejci056d0a82018-12-06 16:57:25 +01004935 LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error);
Radek Krejcib1b59152019-01-07 13:21:56 +01004936 if (uses_status) {
4937
4938 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004939 /* the postponed status check of the node and its real parent - in case of implicit case,
Radek Krejcib1b59152019-01-07 13:21:56 +01004940 * it directly gets the same status flags as the choice;
4941 * uses_status cannot be applied here since uses cannot be child statement of choice */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004942 LY_CHECK_GOTO(lys_compile_status(ctx, &node->flags, cs->flags), error);
Radek Krejci056d0a82018-12-06 16:57:25 +01004943 node->parent = (struct lysc_node*)cs;
4944 } else { /* other than choice */
4945 node->parent = parent;
Radek Krejci19a96102018-11-15 13:38:09 +01004946 }
Radek Krejciec4da802019-05-02 13:02:41 +02004947 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 +01004948 } else {
4949 /* top-level element */
4950 if (!ctx->mod->compiled->data) {
4951 ctx->mod->compiled->data = node;
4952 } else {
4953 /* insert at the end of the module's top-level nodes list */
4954 ctx->mod->compiled->data->prev->next = node;
4955 node->prev = ctx->mod->compiled->data->prev;
4956 ctx->mod->compiled->data->prev = node;
4957 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004958 if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs,
4959 ctx->mod->compiled->notifs, node->name, node)) {
4960 return LY_EVALID;
4961 }
Radek Krejci19a96102018-11-15 13:38:09 +01004962 }
4963
4964 return LY_SUCCESS;
4965
4966error:
4967 lysc_node_free(ctx->ctx, node);
4968 return ret;
4969}
4970
Radek Krejciccd20f12019-02-15 14:12:27 +01004971static void
4972lysc_disconnect(struct lysc_node *node)
4973{
Radek Krejci0a048dd2019-02-18 16:01:43 +01004974 struct lysc_node *parent, *child, *prev = NULL, *next;
4975 struct lysc_node_case *cs = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01004976 int remove_cs = 0;
4977
4978 parent = node->parent;
4979
4980 /* parent's first child */
4981 if (!parent) {
4982 return;
4983 }
4984 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci0a048dd2019-02-18 16:01:43 +01004985 cs = (struct lysc_node_case*)node;
4986 } else if (parent->nodetype == LYS_CASE) {
4987 /* disconnecting some node in a case */
4988 cs = (struct lysc_node_case*)parent;
4989 parent = cs->parent;
4990 for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) {
4991 if (child == node) {
4992 if (cs->child == child) {
4993 if (!child->next || child->next->parent != (struct lysc_node*)cs) {
4994 /* case with a single child -> remove also the case */
4995 child->parent = NULL;
4996 remove_cs = 1;
4997 } else {
4998 cs->child = child->next;
Radek Krejciccd20f12019-02-15 14:12:27 +01004999 }
5000 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005001 break;
Radek Krejciccd20f12019-02-15 14:12:27 +01005002 }
5003 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005004 if (!remove_cs) {
5005 cs = NULL;
5006 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005007 } else if (lysc_node_children(parent, node->flags) == node) {
5008 *lysc_node_children_p(parent, node->flags) = node->next;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005009 }
5010
5011 if (cs) {
5012 if (remove_cs) {
5013 /* cs has only one child which is being also removed */
5014 lysc_disconnect((struct lysc_node*)cs);
5015 lysc_node_free(cs->module->ctx, (struct lysc_node*)cs);
5016 } else {
Radek Krejciccd20f12019-02-15 14:12:27 +01005017 if (((struct lysc_node_choice*)parent)->dflt == cs) {
5018 /* default case removed */
5019 ((struct lysc_node_choice*)parent)->dflt = NULL;
5020 }
5021 if (((struct lysc_node_choice*)parent)->cases == cs) {
5022 /* first case removed */
5023 ((struct lysc_node_choice*)parent)->cases = (struct lysc_node_case*)cs->next;
5024 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005025 if (cs->child) {
5026 /* cs will be removed and disconnected from its siblings, but we have to take care also about its children */
5027 if (cs->child->prev->parent != (struct lysc_node*)cs) {
5028 prev = cs->child->prev;
5029 } /* else all the children are under a single case */
5030 LY_LIST_FOR_SAFE(cs->child, next, child) {
5031 if (child->parent != (struct lysc_node*)cs) {
5032 break;
5033 }
5034 lysc_node_free(node->module->ctx, child);
5035 }
5036 if (prev) {
5037 if (prev->next) {
5038 prev->next = child;
5039 }
5040 if (child) {
5041 child->prev = prev;
5042 } else {
5043 /* link from the first child under the cases */
5044 ((struct lysc_node_choice*)cs->parent)->cases->child->prev = prev;
5045 }
5046 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005047 }
5048 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005049 }
5050
5051 /* siblings */
Radek Krejci0a048dd2019-02-18 16:01:43 +01005052 if (node->prev->next) {
5053 node->prev->next = node->next;
5054 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005055 if (node->next) {
5056 node->next->prev = node->prev;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005057 } else if (node->nodetype != LYS_CASE) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005058 child = (struct lysc_node*)lysc_node_children(parent, node->flags);
Radek Krejci0a048dd2019-02-18 16:01:43 +01005059 if (child) {
5060 child->prev = node->prev;
5061 }
5062 } else if (((struct lysc_node_choice*)parent)->cases) {
5063 ((struct lysc_node_choice*)parent)->cases->prev = node->prev;
Radek Krejciccd20f12019-02-15 14:12:27 +01005064 }
5065}
5066
5067LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005068lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p)
Radek Krejciccd20f12019-02-15 14:12:27 +01005069{
5070 LY_ERR ret = LY_EVALID;
5071 struct ly_set devs_p = {0};
5072 struct ly_set targets = {0};
5073 struct lysc_node *target; /* target target of the deviation */
Radek Krejci7af64242019-02-18 13:07:53 +01005074 struct lysc_node_list *list;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005075 struct lysc_action *rpcs;
5076 struct lysc_notif *notifs;
Radek Krejciccd20f12019-02-15 14:12:27 +01005077 struct lysp_deviation *dev;
5078 struct lysp_deviate *d, **dp_new;
5079 struct lysp_deviate_add *d_add;
5080 struct lysp_deviate_del *d_del;
5081 struct lysp_deviate_rpl *d_rpl;
Radek Krejci7af64242019-02-18 13:07:53 +01005082 unsigned int u, v, x, y, z;
Radek Krejciccd20f12019-02-15 14:12:27 +01005083 struct lysc_deviation {
5084 const char *nodeid;
5085 struct lysc_node *target; /* target node of the deviation */
5086 struct lysp_deviate** deviates;/* sized array of pointers to parsed deviate statements to apply on target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005087 uint16_t flags; /* target's flags from lys_resolve_schema_nodeid() */
Radek Krejciccd20f12019-02-15 14:12:27 +01005088 uint8_t not_supported; /* flag if deviates contains not-supported deviate */
5089 } **devs = NULL;
5090 int i;
5091 size_t prefix_len, name_len;
5092 const char *prefix, *name, *nodeid;
5093 struct lys_module *mod;
Radek Krejci551b12c2019-02-19 16:11:21 +01005094 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005095 uint16_t flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005096
5097 /* get all deviations from the module and all its submodules ... */
5098 LY_ARRAY_FOR(mod_p->deviations, u) {
5099 ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST);
5100 }
5101 LY_ARRAY_FOR(mod_p->includes, v) {
5102 LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) {
5103 ly_set_add(&devs_p, &mod_p->includes[v].submodule->deviations[u], LY_SET_OPT_USEASLIST);
5104 }
5105 }
5106 if (!devs_p.count) {
5107 /* nothing to do */
5108 return LY_SUCCESS;
5109 }
5110
5111 /* ... and group them by the target node */
5112 devs = calloc(devs_p.count, sizeof *devs);
5113 for (u = 0; u < devs_p.count; ++u) {
5114 dev = devs_p.objs[u];
5115
5116 /* resolve the target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005117 LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1,
5118 (const struct lysc_node**)&target, &flags), cleanup);
Radek Krejcif538ce52019-03-05 10:46:14 +01005119 if (target->nodetype == LYS_ACTION) {
5120 /* move the target pointer to input/output to make them different from the action and
5121 * between them. Before the devs[] item is being processed, the target pointer must be fixed
5122 * back to the RPC/action node due to a better compatibility and decision code in this function.
5123 * The LYSC_OPT_INTERNAL is used as a flag to this change. */
5124 if (flags & LYSC_OPT_RPC_INPUT) {
5125 target = (struct lysc_node*)&((struct lysc_action*)target)->input;
5126 flags |= LYSC_OPT_INTERNAL;
5127 } else if (flags & LYSC_OPT_RPC_OUTPUT) {
5128 target = (struct lysc_node*)&((struct lysc_action*)target)->output;
5129 flags |= LYSC_OPT_INTERNAL;
5130 }
5131 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005132 /* insert into the set of targets with duplicity detection */
5133 i = ly_set_add(&targets, target, 0);
5134 if (!devs[i]) {
5135 /* new record */
5136 devs[i] = calloc(1, sizeof **devs);
5137 devs[i]->target = target;
5138 devs[i]->nodeid = dev->nodeid;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005139 devs[i]->flags = flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005140 }
5141 /* add deviates into the deviation's list of deviates */
5142 for (d = dev->deviates; d; d = d->next) {
5143 LY_ARRAY_NEW_GOTO(ctx->ctx, devs[i]->deviates, dp_new, ret, cleanup);
5144 *dp_new = d;
5145 if (d->mod == LYS_DEV_NOT_SUPPORTED) {
5146 devs[i]->not_supported = 1;
5147 }
5148 }
5149 }
5150
5151 /* MACROS for deviates checking */
5152#define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \
5153 if (!(devs[u]->target->nodetype & (NODETYPES))) { \
5154 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), DEVTYPE, PROPERTY);\
5155 goto cleanup; \
5156 }
5157
5158#define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \
5159 if (LY_ARRAY_SIZE(ARRAY) > MAX) { \
5160 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation (%s) of %s with too many (%u) %s properties.", \
5161 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), LY_ARRAY_SIZE(ARRAY), PROPERTY); \
5162 goto cleanup; \
5163 }
5164
5165#define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \
Radek Krejci93dcc392019-02-19 10:43:38 +01005166 if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \
Radek Krejciccd20f12019-02-15 14:12:27 +01005167 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5168 "Invalid deviation (%s) adding \"%s\" property which already exists (with value \"%s\").", \
5169 devs[u]->nodeid, PROPERTY, ((TYPE)devs[u]->target)->VALUEMEMBER); \
5170 goto cleanup; \
5171 }
5172
Radek Krejci551b12c2019-02-19 16:11:21 +01005173#define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5174 if (((TYPE)devs[u]->target)->MEMBER COND) { \
5175 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5176 "Invalid deviation (%s) adding \"%s\" property which already exists (with value \"%u\").", \
5177 devs[u]->nodeid, PROPERTY, ((TYPE)devs[u]->target)->MEMBER); \
5178 goto cleanup; \
5179 }
5180
Radek Krejciccd20f12019-02-15 14:12:27 +01005181#define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \
5182 if (!((TYPE)devs[u]->target)->MEMBER || COND) { \
5183 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, devs[u]->nodeid, DEVTYPE, PROPERTY, VALUE); \
5184 goto cleanup; \
5185 }
5186
Radek Krejci551b12c2019-02-19 16:11:21 +01005187#define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5188 if (!(((TYPE)devs[u]->target)->MEMBER COND)) { \
5189 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5190 "Invalid deviation (%s) replacing with \"%s\" property \"%u\" which is not present.", \
5191 devs[u]->nodeid, PROPERTY, d_rpl->MEMBER); \
5192 goto cleanup; \
5193 }
5194
Radek Krejciccd20f12019-02-15 14:12:27 +01005195#define DEV_DEL_MEMBER(TYPE, MEMBER_TRG, MEMBER_DEV, DELFUNC, PROPERTY) \
5196 DEV_CHECK_PRESENCE(TYPE, 0, MEMBER_TRG, "deleting", PROPERTY, d_del->MEMBER_DEV); \
5197 if (strcmp(((TYPE)devs[u]->target)->MEMBER_TRG, d_del->MEMBER_DEV)) { \
5198 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5199 "Invalid deviation (%s) deleting \"%s\" property \"%s\" which does not match the target's property value \"%s\".", \
5200 devs[u]->nodeid, PROPERTY, d_del->MEMBER_DEV, ((TYPE)devs[u]->target)->MEMBER_TRG); \
5201 goto cleanup; \
5202 } \
5203 DELFUNC(ctx->ctx, ((TYPE)devs[u]->target)->MEMBER_TRG); \
5204 ((TYPE)devs[u]->target)->MEMBER_TRG = NULL;
5205
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005206#define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \
5207 DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d_del->ARRAY_DEV[0]VALMEMBER); \
5208 LY_ARRAY_FOR(d_del->ARRAY_DEV, x) { \
5209 LY_ARRAY_FOR(((TYPE)devs[u]->target)->ARRAY_TRG, y) { \
5210 if (!strcmp(((TYPE)devs[u]->target)->ARRAY_TRG[y]VALMEMBER_CMP, d_del->ARRAY_DEV[x]VALMEMBER)) { break; } \
Radek Krejciccd20f12019-02-15 14:12:27 +01005211 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005212 if (y == LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
Radek Krejciccd20f12019-02-15 14:12:27 +01005213 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5214 "Invalid deviation (%s) deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005215 devs[u]->nodeid, PROPERTY, d_del->ARRAY_DEV[x]VALMEMBER); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005216 goto cleanup; \
5217 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005218 LY_ARRAY_DECREMENT(((TYPE)devs[u]->target)->ARRAY_TRG); \
5219 DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)devs[u]->target)->ARRAY_TRG[y]); \
5220 memmove(&((TYPE)devs[u]->target)->ARRAY_TRG[y], \
5221 &((TYPE)devs[u]->target)->ARRAY_TRG[y + 1], \
5222 (LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG) - y) * (sizeof *((TYPE)devs[u]->target)->ARRAY_TRG)); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005223 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005224 if (!LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
5225 LY_ARRAY_FREE(((TYPE)devs[u]->target)->ARRAY_TRG); \
5226 ((TYPE)devs[u]->target)->ARRAY_TRG = NULL; \
Radek Krejciccd20f12019-02-15 14:12:27 +01005227 }
5228
5229 /* apply deviations */
5230 for (u = 0; u < devs_p.count && devs[u]; ++u) {
Radek Krejcif538ce52019-03-05 10:46:14 +01005231 if (devs[u]->flags & LYSC_OPT_INTERNAL) {
5232 /* fix the target pointer in case of RPC's/action's input/output */
5233 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5234 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, input));
5235 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5236 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, output));
5237 }
5238 }
5239
Radek Krejciccd20f12019-02-15 14:12:27 +01005240 /* not-supported */
5241 if (devs[u]->not_supported) {
5242 if (LY_ARRAY_SIZE(devs[u]->deviates) > 1) {
5243 LOGWRN(ctx->ctx, "Useless multiple (%u) deviates on node \"%s\" since the node is not-supported.",
5244 LY_ARRAY_SIZE(devs[u]->deviates), devs[u]->nodeid);
5245 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02005246
5247#define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \
5248 if (devs[u]->target->parent) { \
5249 ARRAY = (TYPE*)GETFUNC(devs[u]->target->parent); \
5250 } else { \
5251 ARRAY = devs[u]->target->module->compiled->ARRAY; \
5252 } \
5253 LY_ARRAY_FOR(ARRAY, x) { \
5254 if (&ARRAY[x] == (TYPE*)devs[u]->target) { break; } \
5255 } \
5256 if (x < LY_ARRAY_SIZE(ARRAY)) { \
5257 FREEFUNC(ctx->ctx, &ARRAY[x]); \
5258 memmove(&ARRAY[x], &ARRAY[x + 1], (LY_ARRAY_SIZE(ARRAY) - (x + 1)) * sizeof *ARRAY); \
5259 LY_ARRAY_DECREMENT(ARRAY); \
5260 }
5261
Radek Krejcif538ce52019-03-05 10:46:14 +01005262 if (devs[u]->target->nodetype == LYS_ACTION) {
5263 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5264 /* remove RPC's/action's input */
5265 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->input);
5266 memset(&((struct lysc_action*)devs[u]->target)->input, 0, sizeof ((struct lysc_action*)devs[u]->target)->input);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005267 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->input_exts, lysc_ext_instance_free);
5268 ((struct lysc_action*)devs[u]->target)->input_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01005269 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5270 /* remove RPC's/action's output */
5271 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->output);
5272 memset(&((struct lysc_action*)devs[u]->target)->output, 0, sizeof ((struct lysc_action*)devs[u]->target)->output);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005273 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->output_exts, lysc_ext_instance_free);
5274 ((struct lysc_action*)devs[u]->target)->output_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01005275 } else {
5276 /* remove RPC/action */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005277 REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01005278 }
5279 } else if (devs[u]->target->nodetype == LYS_NOTIF) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005280 /* remove Notification */
5281 REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01005282 } else {
5283 /* remove the target node */
5284 lysc_disconnect(devs[u]->target);
5285 lysc_node_free(ctx->ctx, devs[u]->target);
5286 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005287
5288 continue;
5289 }
5290
5291 /* list of deviates (not-supported is not present in the list) */
5292 LY_ARRAY_FOR(devs[u]->deviates, v) {
5293 d = devs[u]->deviates[v];
5294
5295 switch (d->mod) {
5296 case LYS_DEV_ADD:
5297 d_add = (struct lysp_deviate_add*)d;
5298 /* [units-stmt] */
5299 if (d_add->units) {
5300 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units");
5301 DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_UNITS), units, "units", units);
5302
5303 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
5304 DUP_STRING(ctx->ctx, d_add->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
5305 }
5306
5307 /* *must-stmt */
5308 if (d_add->musts) {
5309 switch (devs[u]->target->nodetype) {
5310 case LYS_CONTAINER:
5311 case LYS_LIST:
5312 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_container*)devs[u]->target)->musts,
Radek Krejciec4da802019-05-02 13:02:41 +02005313 x, lys_compile_must, ret, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005314 break;
5315 case LYS_LEAF:
5316 case LYS_LEAFLIST:
5317 case LYS_ANYDATA:
5318 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_leaf*)devs[u]->target)->musts,
Radek Krejciec4da802019-05-02 13:02:41 +02005319 x, lys_compile_must, ret, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005320 break;
5321 case LYS_NOTIF:
Radek Krejcifc11bd72019-04-11 16:00:05 +02005322 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_notif*)devs[u]->target)->musts,
Radek Krejciec4da802019-05-02 13:02:41 +02005323 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005324 break;
5325 case LYS_ACTION:
5326 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5327 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->input.musts,
Radek Krejciec4da802019-05-02 13:02:41 +02005328 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005329 break;
5330 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5331 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->output.musts,
Radek Krejciec4da802019-05-02 13:02:41 +02005332 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005333 break;
5334 }
5335 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01005336 default:
5337 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci7af64242019-02-18 13:07:53 +01005338 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), "add", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005339 goto cleanup;
5340 }
5341 }
5342
5343 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01005344 if (d_add->uniques) {
5345 DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique");
5346 LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d_add->uniques, (struct lysc_node_list*)devs[u]->target), cleanup);
5347 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005348
5349 /* *default-stmt */
5350 if (d_add->dflts) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005351 switch (devs[u]->target->nodetype) {
5352 case LYS_LEAF:
5353 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
5354 DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_DFLT), dflt, "default", dflt);
5355 if (((struct lysc_node_leaf*)devs[u]->target)->dflt) {
5356 /* first, remove the default value taken from the type */
5357 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->dflt);
5358 ((struct lysc_node_leaf*)devs[u]->target)->dflt = NULL;
5359 }
5360 DUP_STRING(ctx->ctx, d_add->dflts[0], ((struct lysc_node_leaf*)devs[u]->target)->dflt);
Radek Krejci551b12c2019-02-19 16:11:21 +01005361 /* mark the new default values as leaf's own */
5362 devs[u]->target->flags |= LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01005363 break;
5364 case LYS_LEAFLIST:
5365 if (((struct lysc_node_leaflist*)devs[u]->target)->dflts && !(devs[u]->target->flags & LYS_SET_DFLT)) {
5366 /* first, remove the default value taken from the type */
5367 LY_ARRAY_FOR(((struct lysc_node_leaflist*)devs[u]->target)->dflts, x) {
5368 lydict_remove(ctx->ctx, ((struct lysc_node_leaflist*)devs[u]->target)->dflts[x]);
5369 }
5370 LY_ARRAY_FREE(((struct lysc_node_leaflist*)devs[u]->target)->dflts);
5371 ((struct lysc_node_leaflist*)devs[u]->target)->dflts = NULL;
5372 }
5373 /* add new default value(s) */
5374 LY_ARRAY_CREATE_GOTO(ctx->ctx, ((struct lysc_node_leaflist*)devs[u]->target)->dflts,
5375 LY_ARRAY_SIZE(d_add->dflts), ret, cleanup);
5376 for (x = y = LY_ARRAY_SIZE(((struct lysc_node_leaflist*)devs[u]->target)->dflts);
5377 x < LY_ARRAY_SIZE(d_add->dflts) + y; ++x) {
5378 DUP_STRING(ctx->ctx, d_add->dflts[x - y], ((struct lysc_node_leaflist*)devs[u]->target)->dflts[x]);
5379 LY_ARRAY_INCREMENT(((struct lysc_node_leaflist*)devs[u]->target)->dflts);
5380 }
Radek Krejci551b12c2019-02-19 16:11:21 +01005381 /* mark the new default values as leaf-list's own */
Radek Krejciccd20f12019-02-15 14:12:27 +01005382 devs[u]->target->flags |= LYS_SET_DFLT;
5383 break;
5384 case LYS_CHOICE:
5385 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
5386 DEV_CHECK_NONPRESENCE(struct lysc_node_choice*, 1, dflt, "default", dflt->name);
5387 /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation
5388 * to allow making the default case even the augmented case from the deviating module */
5389 if (lys_compile_deviation_set_choice_dflt(ctx, devs[u]->nodeid, d_add->dflts[0],
5390 (struct lysc_node_choice*)devs[u]->target)) {
5391 goto cleanup;
5392 }
5393 break;
5394 default:
5395 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci7af64242019-02-18 13:07:53 +01005396 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), "add", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01005397 goto cleanup;
5398 }
5399 }
5400
5401 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01005402 if (d_add->flags & LYS_CONFIG_MASK) {
5403 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_ACTION | LYS_NOTIF)) {
5404 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid,
5405 lys_nodetype2str(devs[u]->target->nodetype), "add", "config");
5406 goto cleanup;
5407 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005408 if (devs[u]->flags) {
5409 LOGWRN(ctx->ctx, "Deviating config inside %s has no effect (%s).",
5410 devs[u]->flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action", devs[u]->nodeid);
5411 }
Radek Krejci93dcc392019-02-19 10:43:38 +01005412 if (devs[u]->target->flags & LYS_SET_CONFIG) {
5413 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5414 "Invalid deviation (%s) adding \"config\" property which already exists (with value \"config %s\").",
5415 devs[u]->nodeid, devs[u]->target->flags & LYS_CONFIG_W ? "true" : "false");
5416 goto cleanup;
5417 }
5418 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_add->flags, devs[u]->nodeid, 0, 0), cleanup);
5419 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005420
5421 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01005422 if (d_add->flags & LYS_MAND_MASK) {
5423 if (devs[u]->target->flags & LYS_MAND_MASK) {
5424 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5425 "Invalid deviation (%s) adding \"mandatory\" property which already exists (with value \"mandatory %s\").",
5426 devs[u]->nodeid, devs[u]->target->flags & LYS_MAND_TRUE ? "true" : "false");
5427 goto cleanup;
5428 }
5429 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_add->flags, devs[u]->nodeid, 0), cleanup);
5430 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005431
5432 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005433 if (d_add->flags & LYS_SET_MIN) {
5434 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5435 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
5436 /* change value */
5437 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_add->min;
5438 } else if (devs[u]->target->nodetype == LYS_LIST) {
5439 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
5440 /* change value */
5441 ((struct lysc_node_list*)devs[u]->target)->min = d_add->min;
5442 } else {
5443 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid,
5444 lys_nodetype2str(devs[u]->target->nodetype), "add", "min-elements");
5445 goto cleanup;
5446 }
5447 if (d_add->min) {
5448 devs[u]->target->flags |= LYS_MAND_TRUE;
5449 }
5450 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005451
5452 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005453 if (d_add->flags & LYS_SET_MAX) {
5454 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5455 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
5456 /* change value */
5457 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
5458 } else if (devs[u]->target->nodetype == LYS_LIST) {
5459 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
5460 /* change value */
5461 ((struct lysc_node_list*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
5462 } else {
5463 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid,
5464 lys_nodetype2str(devs[u]->target->nodetype), "add", "max-elements");
5465 goto cleanup;
5466 }
5467 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005468
5469 break;
5470 case LYS_DEV_DELETE:
5471 d_del = (struct lysp_deviate_del*)d;
5472
5473 /* [units-stmt] */
5474 if (d_del->units) {
5475 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units");
5476 DEV_DEL_MEMBER(struct lysc_node_leaf*, units, units, lydict_remove, "units");
5477 }
5478
5479 /* *must-stmt */
5480 if (d_del->musts) {
5481 switch (devs[u]->target->nodetype) {
5482 case LYS_CONTAINER:
5483 case LYS_LIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005484 DEV_DEL_ARRAY(struct lysc_node_container*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005485 break;
5486 case LYS_LEAF:
5487 case LYS_LEAFLIST:
5488 case LYS_ANYDATA:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005489 DEV_DEL_ARRAY(struct lysc_node_leaf*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005490 break;
5491 case LYS_NOTIF:
Radek Krejcifc11bd72019-04-11 16:00:05 +02005492 DEV_DEL_ARRAY(struct lysc_notif*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005493 break;
5494 case LYS_ACTION:
5495 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5496 DEV_DEL_ARRAY(struct lysc_action*, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
5497 break;
5498 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5499 DEV_DEL_ARRAY(struct lysc_action*, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
5500 break;
5501 }
5502 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01005503 default:
5504 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci7af64242019-02-18 13:07:53 +01005505 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), "delete", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005506 goto cleanup;
5507 }
5508 }
5509
5510 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01005511 if (d_del->uniques) {
5512 DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique");
5513 list = (struct lysc_node_list*)devs[u]->target; /* shortcut */
5514 LY_ARRAY_FOR(d_del->uniques, x) {
5515 LY_ARRAY_FOR(list->uniques, z) {
5516 for (name = d_del->uniques[x], y = 0; name; name = nodeid, ++y) {
5517 nodeid = strpbrk(name, " \t\n");
5518 if (nodeid) {
5519 if (strncmp(name, list->uniques[z][y]->name, nodeid - name)
5520 || list->uniques[z][y]->name[nodeid - name] != '\0') {
5521 break;
5522 }
5523 while (isspace(*nodeid)) {
5524 ++nodeid;
5525 }
5526 } else {
5527 if (strcmp(name, list->uniques[z][y]->name)) {
5528 break;
5529 }
5530 }
5531 }
5532 if (!name) {
5533 /* complete match - remove the unique */
5534 LY_ARRAY_DECREMENT(list->uniques);
5535 LY_ARRAY_FREE(list->uniques[z]);
5536 memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_SIZE(list->uniques) - z) * (sizeof *list->uniques));
5537 --z;
5538 break;
5539 }
5540 }
5541 if (!list->uniques || z == LY_ARRAY_SIZE(list->uniques)) {
5542 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5543 "Invalid deviation (%s) deleting \"unique\" property \"%s\" which does not match any of the target's property values.",
5544 devs[u]->nodeid, d_del->uniques[x]);
5545 goto cleanup;
5546 }
5547 }
5548 if (!LY_ARRAY_SIZE(list->uniques)) {
5549 LY_ARRAY_FREE(list->uniques);
5550 list->uniques = NULL;
5551 }
5552 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005553
5554 /* *default-stmt */
5555 if (d_del->dflts) {
5556 switch (devs[u]->target->nodetype) {
5557 case LYS_LEAF:
5558 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
5559 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
5560 dflt, "deleting", "default", d_del->dflts[0]);
5561
5562 DEV_DEL_MEMBER(struct lysc_node_leaf*, dflt, dflts[0], lydict_remove, "default");
Radek Krejci551b12c2019-02-19 16:11:21 +01005563 devs[u]->target->flags &= ~LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01005564 break;
5565 case LYS_LEAFLIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005566 DEV_DEL_ARRAY(struct lysc_node_leaflist*, dflts, dflts, , , , lydict_remove, "default");
Radek Krejci551b12c2019-02-19 16:11:21 +01005567 if (!((struct lysc_node_leaflist*)devs[u]->target)->dflts) {
5568 devs[u]->target->flags &= ~LYS_SET_DFLT;
5569 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005570 break;
5571 case LYS_CHOICE:
5572 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
5573 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "deleting", "default", d_del->dflts[0]);
5574 nodeid = d_del->dflts[0];
5575 LY_CHECK_GOTO(lys_parse_nodeid(&nodeid, &prefix, &prefix_len, &name, &name_len), cleanup);
5576 if (prefix) {
5577 /* use module prefixes from the deviation module to match the module of the default case */
5578 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
5579 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
5580 "Invalid deviation (%s) deleting \"default\" property \"%s\" of choice. "
5581 "The prefix does not match any imported module of the deviation module.",
5582 devs[u]->nodeid, d_del->dflts[0]);
5583 goto cleanup;
5584 }
5585 if (mod != ((struct lysc_node_choice*)devs[u]->target)->dflt->module) {
5586 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
5587 "Invalid deviation (%s) deleting \"default\" property \"%s\" of choice. "
5588 "The prefix does not match the default case's module.",
5589 devs[u]->nodeid, d_del->dflts[0]);
5590 goto cleanup;
5591 }
5592 }
5593 /* else {
5594 * strictly, the default prefix would point to the deviation module, but the value should actually
5595 * match the default string in the original module (usually unprefixed), so in this case we do not check
5596 * the module of the default case, just matching its name */
5597 if (strcmp(name, ((struct lysc_node_choice*)devs[u]->target)->dflt->name)) {
5598 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5599 "Invalid deviation (%s) deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".",
5600 devs[u]->nodeid, d_del->dflts[0], ((struct lysc_node_choice*)devs[u]->target)->dflt->name);
5601 goto cleanup;
5602 }
5603 ((struct lysc_node_choice*)devs[u]->target)->dflt->flags &= ~LYS_SET_DFLT;
5604 ((struct lysc_node_choice*)devs[u]->target)->dflt = NULL;
5605 break;
5606 default:
5607 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci7af64242019-02-18 13:07:53 +01005608 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), "delete", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01005609 goto cleanup;
5610 }
5611 }
5612
5613 break;
5614 case LYS_DEV_REPLACE:
5615 d_rpl = (struct lysp_deviate_rpl*)d;
5616
5617 /* [type-stmt] */
Radek Krejci33f72892019-02-21 10:36:58 +01005618 if (d_rpl->type) {
5619 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type");
5620 /* type is mandatory, so checking for its presence is not necessary */
5621 lysc_type_free(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->type);
Radek Krejciec4da802019-05-02 13:02:41 +02005622 LY_CHECK_GOTO(lys_compile_node_type(ctx, NULL, d_rpl->type, (struct lysc_node_leaf*)devs[u]->target), cleanup);
Radek Krejci33f72892019-02-21 10:36:58 +01005623 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005624
5625 /* [units-stmt] */
5626 if (d_rpl->units) {
5627 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units");
5628 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_UNITS),
5629 units, "replacing", "units", d_rpl->units);
5630
5631 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
5632 DUP_STRING(ctx->ctx, d_rpl->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
5633 }
5634
5635 /* [default-stmt] */
5636 if (d_rpl->dflt) {
5637 switch (devs[u]->target->nodetype) {
5638 case LYS_LEAF:
5639 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
5640 dflt, "replacing", "default", d_rpl->dflt);
5641
5642 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->dflt);
5643 DUP_STRING(ctx->ctx, d_rpl->dflt, ((struct lysc_node_leaf*)devs[u]->target)->dflt);
5644 break;
5645 case LYS_CHOICE:
5646 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "replacing", "default", d_rpl->dflt);
5647 if (lys_compile_deviation_set_choice_dflt(ctx, devs[u]->nodeid, d_rpl->dflt,
5648 (struct lysc_node_choice*)devs[u]->target)) {
5649 goto cleanup;
5650 }
5651 break;
5652 default:
5653 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci7af64242019-02-18 13:07:53 +01005654 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), "replace", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01005655 goto cleanup;
5656 }
5657 }
5658
5659 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01005660 if (d_rpl->flags & LYS_CONFIG_MASK) {
5661 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_ACTION | LYS_NOTIF)) {
5662 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid,
5663 lys_nodetype2str(devs[u]->target->nodetype), "replace", "config");
5664 goto cleanup;
5665 }
5666 if (!(devs[u]->target->flags & LYS_SET_CONFIG)) {
5667 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, devs[u]->nodeid,
5668 "replacing", "config", d_rpl->flags & LYS_CONFIG_W ? "config true" : "config false");
5669 goto cleanup;
5670 }
5671 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_rpl->flags, devs[u]->nodeid, 0, 0), cleanup);
5672 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005673
5674 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01005675 if (d_rpl->flags & LYS_MAND_MASK) {
5676 if (!(devs[u]->target->flags & LYS_MAND_MASK)) {
5677 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, devs[u]->nodeid,
5678 "replacing", "mandatory", d_rpl->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false");
5679 goto cleanup;
5680 }
5681 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_rpl->flags, devs[u]->nodeid, 0), cleanup);
5682 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005683
5684 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005685 if (d_rpl->flags & LYS_SET_MIN) {
5686 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5687 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
5688 /* change value */
5689 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_rpl->min;
5690 } else if (devs[u]->target->nodetype == LYS_LIST) {
5691 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
5692 /* change value */
5693 ((struct lysc_node_list*)devs[u]->target)->min = d_rpl->min;
5694 } else {
5695 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid,
5696 lys_nodetype2str(devs[u]->target->nodetype), "replace", "min-elements");
5697 goto cleanup;
5698 }
5699 if (d_rpl->min) {
5700 devs[u]->target->flags |= LYS_MAND_TRUE;
5701 }
5702 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005703
5704 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005705 if (d_rpl->flags & LYS_SET_MAX) {
5706 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5707 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
5708 /* change value */
5709 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
5710 } else if (devs[u]->target->nodetype == LYS_LIST) {
5711 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
5712 /* change value */
5713 ((struct lysc_node_list*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
5714 } else {
5715 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid,
5716 lys_nodetype2str(devs[u]->target->nodetype), "replace", "max-elements");
5717 goto cleanup;
5718 }
5719 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005720
5721 break;
5722 default:
5723 LOGINT(ctx->ctx);
5724 goto cleanup;
5725 }
5726 }
Radek Krejci551b12c2019-02-19 16:11:21 +01005727
Radek Krejci33f72892019-02-21 10:36:58 +01005728 /* final check when all deviations of a single target node are applied */
5729
Radek Krejci551b12c2019-02-19 16:11:21 +01005730 /* check min-max compatibility */
5731 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5732 min = ((struct lysc_node_leaflist*)devs[u]->target)->min;
5733 max = ((struct lysc_node_leaflist*)devs[u]->target)->max;
5734 } else if (devs[u]->target->nodetype == LYS_LIST) {
5735 min = ((struct lysc_node_list*)devs[u]->target)->min;
5736 max = ((struct lysc_node_list*)devs[u]->target)->max;
5737 } else {
5738 min = max = 0;
5739 }
5740 if (min > max) {
5741 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid combination of min-elements and max-elements "
5742 "after deviation (%s): min value %u is bigger than max value %u.",
5743 devs[u]->nodeid, min, max);
5744 goto cleanup;
5745 }
5746
5747 /* check mandatory - default compatibility */
5748 if ((devs[u]->target->nodetype & (LYS_LEAF | LYS_LEAFLIST))
5749 && (devs[u]->target->flags & LYS_SET_DFLT)
5750 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
5751 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5752 "Invalid deviation (%s) combining default value and mandatory %s.",
5753 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype));
5754 goto cleanup;
5755 } else if ((devs[u]->target->nodetype & LYS_CHOICE)
5756 && ((struct lysc_node_choice*)devs[u]->target)->dflt
5757 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
5758 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5759 "Invalid deviation (%s) combining default case and mandatory choice.", devs[u]->nodeid);
5760 goto cleanup;
5761 }
5762 if (devs[u]->target->parent && (devs[u]->target->parent->flags & LYS_SET_DFLT) && (devs[u]->target->flags & LYS_MAND_TRUE)) {
5763 /* mandatory node under a default case */
5764 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5765 "Invalid deviation (%s) combining mandatory %s \"%s\" in a default choice's case \"%s\".",
5766 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), devs[u]->target->name, devs[u]->target->parent->name);
5767 goto cleanup;
5768 }
Radek Krejci33f72892019-02-21 10:36:58 +01005769
5770 /* TODO check default value(s) according to the type */
Radek Krejciccd20f12019-02-15 14:12:27 +01005771 }
5772
5773 ret = LY_SUCCESS;
5774
5775cleanup:
5776 for (u = 0; u < devs_p.count && devs[u]; ++u) {
5777 LY_ARRAY_FREE(devs[u]->deviates);
5778 free(devs[u]);
5779 }
5780 free(devs);
5781 ly_set_erase(&targets, NULL);
5782 ly_set_erase(&devs_p, NULL);
5783
5784 return ret;
5785}
5786
Radek Krejcib56c7502019-02-13 14:19:54 +01005787/**
Radek Krejcid05cbd92018-12-05 14:26:40 +01005788 * @brief Compile the given YANG submodule into the main module.
5789 * @param[in] ctx Compile context
5790 * @param[in] inc Include structure from the main module defining the submodule.
Radek Krejcid05cbd92018-12-05 14:26:40 +01005791 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
5792 */
5793LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005794lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc)
Radek Krejcid05cbd92018-12-05 14:26:40 +01005795{
5796 unsigned int u;
5797 LY_ERR ret = LY_SUCCESS;
5798 /* shortcuts */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01005799 struct lysp_submodule *submod = inc->submodule;
Radek Krejcid05cbd92018-12-05 14:26:40 +01005800 struct lysc_module *mainmod = ctx->mod->compiled;
5801
Radek Krejci0af46292019-01-11 16:02:31 +01005802 if (!mainmod->mod->off_features) {
5803 /* features are compiled directly into the compiled module structure,
5804 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves.
5805 * The features compilation is finished in the main module (lys_compile()). */
Radek Krejci693262f2019-04-29 15:23:20 +02005806 ret = lys_feature_precompile(ctx->ctx, ctx->mod, submod->features,
Radek Krejci0af46292019-01-11 16:02:31 +01005807 mainmod->mod->off_features ? &mainmod->mod->off_features : &mainmod->features);
5808 LY_CHECK_GOTO(ret, error);
5809 }
5810
Radek Krejciec4da802019-05-02 13:02:41 +02005811 COMPILE_ARRAY_UNIQUE_GOTO(ctx, submod->identities, mainmod->identities, u, lys_compile_identity, ret, error);
Radek Krejcid05cbd92018-12-05 14:26:40 +01005812
Radek Krejci8cce8532019-03-05 11:27:45 +01005813 /* TODO data nodes */
5814
Radek Krejciec4da802019-05-02 13:02:41 +02005815 COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, u, lys_compile_action, 0, ret, error);
5816 COMPILE_ARRAY1_GOTO(ctx, submod->notifs, mainmod->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejci8cce8532019-03-05 11:27:45 +01005817
Radek Krejcid05cbd92018-12-05 14:26:40 +01005818error:
5819 return ret;
5820}
5821
Radek Krejci19a96102018-11-15 13:38:09 +01005822LY_ERR
5823lys_compile(struct lys_module *mod, int options)
5824{
5825 struct lysc_ctx ctx = {0};
5826 struct lysc_module *mod_c;
Radek Krejci412ddfa2018-11-23 11:44:11 +01005827 struct lysc_type *type, *typeiter;
Radek Krejci19a96102018-11-15 13:38:09 +01005828 struct lysp_module *sp;
5829 struct lysp_node *node_p;
Radek Krejci95710c92019-02-11 15:49:55 +01005830 struct lysp_augment **augments = NULL;
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005831 struct lysp_grp *grps;
Radek Krejci95710c92019-02-11 15:49:55 +01005832 struct lys_module *m;
Radek Krejcicdfecd92018-11-26 11:27:32 +01005833 unsigned int u, v;
Radek Krejcid05cbd92018-12-05 14:26:40 +01005834 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01005835
Radek Krejci0bcdaed2019-01-10 10:21:34 +01005836 LY_CHECK_ARG_RET(NULL, mod, mod->parsed, mod->ctx, LY_EINVAL);
Radek Krejci096235c2019-01-11 11:12:19 +01005837
5838 if (!mod->implemented) {
5839 /* just imported modules are not compiled */
5840 return LY_SUCCESS;
5841 }
5842
Radek Krejci19a96102018-11-15 13:38:09 +01005843 sp = mod->parsed;
5844
Radek Krejci0bcdaed2019-01-10 10:21:34 +01005845 ctx.ctx = mod->ctx;
Radek Krejci19a96102018-11-15 13:38:09 +01005846 ctx.mod = mod;
Radek Krejcie86bf772018-12-14 11:39:53 +01005847 ctx.mod_def = mod;
Radek Krejciec4da802019-05-02 13:02:41 +02005848 ctx.options = options;
Radek Krejci19a96102018-11-15 13:38:09 +01005849
5850 mod->compiled = mod_c = calloc(1, sizeof *mod_c);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01005851 LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM);
5852 mod_c->mod = mod;
Radek Krejci19a96102018-11-15 13:38:09 +01005853
Radek Krejciec4da802019-05-02 13:02:41 +02005854 COMPILE_ARRAY_GOTO(&ctx, sp->imports, mod_c->imports, u, lys_compile_import, ret, error);
Radek Krejcid05cbd92018-12-05 14:26:40 +01005855 LY_ARRAY_FOR(sp->includes, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02005856 ret = lys_compile_submodule(&ctx, &sp->includes[u]);
Radek Krejcid05cbd92018-12-05 14:26:40 +01005857 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
5858 }
Radek Krejci0af46292019-01-11 16:02:31 +01005859 if (mod->off_features) {
5860 /* there is already precompiled array of features */
5861 mod_c->features = mod->off_features;
5862 mod->off_features = NULL;
Radek Krejci0af46292019-01-11 16:02:31 +01005863 } else {
5864 /* features are compiled directly into the compiled module structure,
5865 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves */
Radek Krejci693262f2019-04-29 15:23:20 +02005866 ret = lys_feature_precompile(ctx.ctx, ctx.mod, sp->features, &mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01005867 LY_CHECK_GOTO(ret, error);
5868 }
5869 /* finish feature compilation, not only for the main module, but also for the submodules.
5870 * Due to possible forward references, it must be done when all the features (including submodules)
5871 * are present. */
5872 LY_ARRAY_FOR(sp->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02005873 ret = lys_feature_precompile_finish(&ctx, &sp->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01005874 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
5875 }
5876 LY_ARRAY_FOR(sp->includes, v) {
5877 LY_ARRAY_FOR(sp->includes[v].submodule->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02005878 ret = lys_feature_precompile_finish(&ctx, &sp->includes[v].submodule->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01005879 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
5880 }
5881 }
5882
Radek Krejciec4da802019-05-02 13:02:41 +02005883 COMPILE_ARRAY_UNIQUE_GOTO(&ctx, sp->identities, mod_c->identities, u, lys_compile_identity, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01005884 if (sp->identities) {
5885 LY_CHECK_RET(lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities));
5886 }
5887
Radek Krejci95710c92019-02-11 15:49:55 +01005888 /* data nodes */
Radek Krejci19a96102018-11-15 13:38:09 +01005889 LY_LIST_FOR(sp->data, node_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02005890 ret = lys_compile_node(&ctx, node_p, NULL, 0);
Radek Krejci19a96102018-11-15 13:38:09 +01005891 LY_CHECK_GOTO(ret, error);
5892 }
Radek Krejci95710c92019-02-11 15:49:55 +01005893
Radek Krejciec4da802019-05-02 13:02:41 +02005894 COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error);
5895 COMPILE_ARRAY1_GOTO(&ctx, sp->notifs, mod_c->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejciccd20f12019-02-15 14:12:27 +01005896
Radek Krejci95710c92019-02-11 15:49:55 +01005897 /* augments - sort first to cover augments augmenting other augments */
Radek Krejci3641f562019-02-13 15:38:40 +01005898 ret = lys_compile_augment_sort(&ctx, sp->augments, sp->includes, &augments);
Radek Krejci95710c92019-02-11 15:49:55 +01005899 LY_CHECK_GOTO(ret, error);
5900 LY_ARRAY_FOR(augments, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02005901 ret = lys_compile_augment(&ctx, augments[u], NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01005902 LY_CHECK_GOTO(ret, error);
5903 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005904
5905 /* deviations */
Radek Krejciec4da802019-05-02 13:02:41 +02005906 ret = lys_compile_deviations(&ctx, sp);
Radek Krejciccd20f12019-02-15 14:12:27 +01005907 LY_CHECK_GOTO(ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01005908
Radek Krejciec4da802019-05-02 13:02:41 +02005909 COMPILE_ARRAY_GOTO(&ctx, sp->exts, mod_c->exts, u, lys_compile_ext, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01005910
Radek Krejcia3045382018-11-22 14:30:31 +01005911 /* validate leafref's paths and when/must xpaths */
Radek Krejci412ddfa2018-11-23 11:44:11 +01005912 /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which
5913 * can be also leafref, in case it is already resolved, go through the chain and check that it does not
5914 * 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 +01005915 for (u = 0; u < ctx.unres.count; ++u) {
Radek Krejci0e5d8382018-11-28 16:37:53 +01005916 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Radek Krejcia3045382018-11-22 14:30:31 +01005917 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
5918 if (type->basetype == LY_TYPE_LEAFREF) {
5919 /* validate the path */
Radek Krejci412ddfa2018-11-23 11:44:11 +01005920 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 +01005921 LY_CHECK_GOTO(ret, error);
Radek Krejcicdfecd92018-11-26 11:27:32 +01005922 } else if (type->basetype == LY_TYPE_UNION) {
5923 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
5924 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
5925 /* validate the path */
5926 ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[u]),
5927 (struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v]);
5928 LY_CHECK_GOTO(ret, error);
5929 }
5930 }
Radek Krejcia3045382018-11-22 14:30:31 +01005931 }
5932 }
5933 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01005934 for (u = 0; u < ctx.unres.count; ++u) {
Radek Krejci0e5d8382018-11-28 16:37:53 +01005935 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Radek Krejci412ddfa2018-11-23 11:44:11 +01005936 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
5937 if (type->basetype == LY_TYPE_LEAFREF) {
5938 /* store pointer to the real type */
5939 for (typeiter = ((struct lysc_type_leafref*)type)->realtype;
5940 typeiter->basetype == LY_TYPE_LEAFREF;
5941 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
5942 ((struct lysc_type_leafref*)type)->realtype = typeiter;
Radek Krejcicdfecd92018-11-26 11:27:32 +01005943 } else if (type->basetype == LY_TYPE_UNION) {
5944 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
5945 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
5946 /* store pointer to the real type */
5947 for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype;
5948 typeiter->basetype == LY_TYPE_LEAFREF;
5949 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
5950 ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter;
5951 }
5952 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01005953 }
5954 }
5955 }
Radek Krejciec4da802019-05-02 13:02:41 +02005956
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005957 /* validate non-instantiated groupings from the parsed schema,
5958 * without it we would accept even the schemas with invalid grouping specification */
5959 ctx.options |= LYSC_OPT_GROUPING;
5960 LY_ARRAY_FOR(sp->groupings, u) {
5961 if (!(sp->groupings[u].flags & LYS_USED_GRP)) {
5962 LY_CHECK_GOTO((ret = lys_compile_grouping(&ctx, node_p, &sp->groupings[u])) != LY_SUCCESS, error);
5963 }
5964 }
5965 LY_LIST_FOR(sp->data, node_p) {
5966 grps = (struct lysp_grp*)lysp_node_groupings(node_p);
5967 LY_ARRAY_FOR(grps, u) {
5968 if (!(grps[u].flags & LYS_USED_GRP)) {
5969 LY_CHECK_GOTO((ret = lys_compile_grouping(&ctx, node_p, &grps[u])) != LY_SUCCESS, error);
5970 }
5971 }
5972 }
5973
Radek Krejcia3045382018-11-22 14:30:31 +01005974 ly_set_erase(&ctx.unres, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01005975 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02005976 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01005977 LY_ARRAY_FREE(augments);
Radek Krejcia3045382018-11-22 14:30:31 +01005978
Radek Krejciec4da802019-05-02 13:02:41 +02005979 if (ctx.options & LYSC_OPT_FREE_SP) {
Radek Krejci19a96102018-11-15 13:38:09 +01005980 lysp_module_free(mod->parsed);
5981 ((struct lys_module*)mod)->parsed = NULL;
5982 }
5983
Radek Krejciec4da802019-05-02 13:02:41 +02005984 if (!(ctx.options & LYSC_OPT_INTERNAL)) {
Radek Krejci95710c92019-02-11 15:49:55 +01005985 /* remove flag of the modules implemented by dependency */
5986 for (u = 0; u < ctx.ctx->list.count; ++u) {
5987 m = ctx.ctx->list.objs[u];
5988 if (m->implemented == 2) {
5989 m->implemented = 1;
5990 }
5991 }
5992 }
5993
Radek Krejci19a96102018-11-15 13:38:09 +01005994 ((struct lys_module*)mod)->compiled = mod_c;
5995 return LY_SUCCESS;
5996
5997error:
Radek Krejci95710c92019-02-11 15:49:55 +01005998 lys_feature_precompile_revert(&ctx, mod);
Radek Krejcia3045382018-11-22 14:30:31 +01005999 ly_set_erase(&ctx.unres, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01006000 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02006001 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01006002 LY_ARRAY_FREE(augments);
Radek Krejci19a96102018-11-15 13:38:09 +01006003 lysc_module_free(mod_c, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01006004 mod->compiled = NULL;
6005
6006 /* revert compilation of modules implemented by dependency */
6007 for (u = 0; u < ctx.ctx->list.count; ++u) {
6008 m = ctx.ctx->list.objs[u];
6009 if (m->implemented == 2) {
6010 /* revert features list to the precompiled state */
6011 lys_feature_precompile_revert(&ctx, m);
6012 /* mark module as imported-only / not-implemented */
6013 m->implemented = 0;
6014 /* free the compiled version of the module */
6015 lysc_module_free(m->compiled, NULL);
6016 m->compiled = NULL;
6017 }
6018 }
6019
Radek Krejci19a96102018-11-15 13:38:09 +01006020 return ret;
6021}