blob: 47e759f17c6778f1c9bdebb73382afa2c5fad3e2 [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
Radek Krejcie7b95092019-05-15 11:03:07 +020017#include <assert.h>
Radek Krejci19a96102018-11-15 13:38:09 +010018#include <ctype.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020019#include <stddef.h>
20#include <stdint.h>
Radek Krejci19a96102018-11-15 13:38:09 +010021#include <stdio.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020022#include <stdlib.h>
23#include <string.h>
Radek Krejci19a96102018-11-15 13:38:09 +010024
Radek Krejcie7b95092019-05-15 11:03:07 +020025#include "dict.h"
26#include "log.h"
Radek Krejci0935f412019-08-20 16:15:18 +020027#include "plugins_exts.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020028#include "set.h"
29#include "plugins_types.h"
Radek Krejci0935f412019-08-20 16:15:18 +020030#include "plugins_exts_internal.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020031#include "tree.h"
32#include "tree_schema.h"
Radek Krejci19a96102018-11-15 13:38:09 +010033#include "tree_schema_internal.h"
34#include "xpath.h"
35
36/**
37 * @brief Duplicate string into dictionary
38 * @param[in] CTX libyang context of the dictionary.
39 * @param[in] ORIG String to duplicate.
40 * @param[out] DUP Where to store the result.
41 */
42#define DUP_STRING(CTX, ORIG, DUP) if (ORIG) {DUP = lydict_insert(CTX, ORIG, 0);}
43
Radek Krejciec4da802019-05-02 13:02:41 +020044#define COMPILE_ARRAY_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, FUNC, RET, GOTO) \
Radek Krejci19a96102018-11-15 13:38:09 +010045 if (ARRAY_P) { \
46 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \
Radek Krejcid05cbd92018-12-05 14:26:40 +010047 size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
Radek Krejci19a96102018-11-15 13:38:09 +010048 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], &(ARRAY_C)[ITER + __array_offset]); \
Radek Krejcid05cbd92018-12-05 14:26:40 +010051 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
52 } \
53 }
54
Radek Krejciec4da802019-05-02 13:02:41 +020055#define COMPILE_ARRAY1_GOTO(CTX, ARRAY_P, ARRAY_C, PARENT, ITER, FUNC, USES_STATUS, RET, GOTO) \
Radek Krejci6eeb58f2019-02-22 16:29:37 +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], PARENT, &(ARRAY_C)[ITER + __array_offset], USES_STATUS); \
Radek Krejci6eeb58f2019-02-22 16:29:37 +010062 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
63 } \
64 }
65
Radek Krejci0935f412019-08-20 16:15:18 +020066#define COMPILE_EXTS_GOTO(CTX, EXTS_P, EXT_C, PARENT, PARENT_TYPE, RET, GOTO) \
67 if (EXTS_P) { \
68 LY_ARRAY_CREATE_GOTO((CTX)->ctx, EXT_C, LY_ARRAY_SIZE(EXTS_P), RET, GOTO); \
69 for (uint32_t __exts_iter = 0, __array_offset = LY_ARRAY_SIZE(EXT_C); __exts_iter < LY_ARRAY_SIZE(EXTS_P); ++__exts_iter) { \
70 LY_ARRAY_INCREMENT(EXT_C); \
71 RET = lys_compile_ext(CTX, &(EXTS_P)[__exts_iter], &(EXT_C)[__exts_iter + __array_offset], PARENT, PARENT_TYPE); \
72 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
73 } \
74 }
75
Radek Krejciec4da802019-05-02 13:02:41 +020076#define COMPILE_ARRAY_UNIQUE_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, FUNC, RET, GOTO) \
Radek Krejcid05cbd92018-12-05 14:26:40 +010077 if (ARRAY_P) { \
78 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \
79 size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
80 for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \
81 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020082 RET = FUNC(CTX, &(ARRAY_P)[ITER], ARRAY_C, &(ARRAY_C)[ITER + __array_offset]); \
Radek Krejci19a96102018-11-15 13:38:09 +010083 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
84 } \
85 }
86
Radek Krejciec4da802019-05-02 13:02:41 +020087#define COMPILE_MEMBER_GOTO(CTX, MEMBER_P, MEMBER_C, FUNC, RET, GOTO) \
Radek Krejci19a96102018-11-15 13:38:09 +010088 if (MEMBER_P) { \
89 MEMBER_C = calloc(1, sizeof *(MEMBER_C)); \
90 LY_CHECK_ERR_GOTO(!(MEMBER_C), LOGMEM((CTX)->ctx); RET = LY_EMEM, GOTO); \
Radek Krejciec4da802019-05-02 13:02:41 +020091 RET = FUNC(CTX, MEMBER_P, MEMBER_C); \
Radek Krejci19a96102018-11-15 13:38:09 +010092 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
93 }
94
Radek Krejciec4da802019-05-02 13:02:41 +020095#define COMPILE_MEMBER_ARRAY_GOTO(CTX, MEMBER_P, ARRAY_C, FUNC, RET, GOTO) \
Radek Krejci00b874b2019-02-12 10:54:50 +010096 if (MEMBER_P) { \
97 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, 1, RET, GOTO); \
98 size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
99 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +0200100 RET = FUNC(CTX, MEMBER_P, &(ARRAY_C)[__array_offset]); \
Radek Krejci00b874b2019-02-12 10:54:50 +0100101 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
102 }
103
Radek Krejcid05cbd92018-12-05 14:26:40 +0100104#define COMPILE_CHECK_UNIQUENESS(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \
105 if (ARRAY) { \
Radek Krejci0af46292019-01-11 16:02:31 +0100106 for (unsigned int u__ = 0; u__ < LY_ARRAY_SIZE(ARRAY); ++u__) { \
107 if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__].MEMBER) == (void*)(IDENT)) { \
Radek Krejcid05cbd92018-12-05 14:26:40 +0100108 LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \
109 return LY_EVALID; \
110 } \
111 } \
112 }
113
Radek Krejci19a96102018-11-15 13:38:09 +0100114static struct lysc_ext_instance *
115lysc_ext_instance_dup(struct ly_ctx *ctx, struct lysc_ext_instance *orig)
116{
Radek Krejcie7b95092019-05-15 11:03:07 +0200117 /* TODO - extensions */
Radek Krejci19a96102018-11-15 13:38:09 +0100118 (void) ctx;
119 (void) orig;
120 return NULL;
121}
122
Radek Krejcib56c7502019-02-13 14:19:54 +0100123/**
Radek Krejci474f9b82019-07-24 11:36:37 +0200124 * @brief Add record into the compile context's list of incomplete default values.
125 * @param[in] ctx Compile context with the incomplete default values list.
126 * @param[in] context_node Context schema node to store in the record.
127 * @param[in] dflt Incomplete default value to store in the record.
128 * @param[in] dflt_mod Module of the default value definition to store in the record.
129 * @return LY_EMEM in case of memory allocation failure.
130 * @return LY_SUCCESS
131 */
132static LY_ERR
133lysc_incomplete_dflts_add(struct lysc_ctx *ctx, struct lysc_node *context_node, struct lyd_value *dflt, struct lys_module *dflt_mod)
134{
135 struct lysc_incomplete_dflt *r;
136 r = malloc(sizeof *r);
137 LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM);
138 r->context_node = context_node;
139 r->dflt = dflt;
140 r->dflt_mod = dflt_mod;
141 ly_set_add(&ctx->dflts, r, LY_SET_OPT_USEASLIST);
142
143 return LY_SUCCESS;
144}
145
146/**
147 * @brief Remove record of the given default value from the compile context's list of incomplete default values.
148 * @param[in] ctx Compile context with the incomplete default values list.
149 * @param[in] dflt Incomplete default values identifying the record to remove.
150 */
151static void
152lysc_incomplete_dflts_remove(struct lysc_ctx *ctx, struct lyd_value *dflt)
153{
154 unsigned int u;
155 struct lysc_incomplete_dflt *r;
156
157 for (u = 0; u < ctx->dflts.count; ++u) {
158 r = (struct lysc_incomplete_dflt*)ctx->dflts.objs[u];
159 if (r->dflt == dflt) {
160 free(ctx->dflts.objs[u]);
161 memmove(&ctx->dflts.objs[u], &ctx->dflts.objs[u + 1], (ctx->dflts.count - (u + 1)) * sizeof *ctx->dflts.objs);
162 --ctx->dflts.count;
163 return;
164 }
165 }
166}
167
Radek Krejci0e59c312019-08-15 15:34:15 +0200168void
Radek Krejci327de162019-06-14 12:52:07 +0200169lysc_update_path(struct lysc_ctx *ctx, struct lysc_node *parent, const char *name)
170{
171 int len;
172 int nextlevel = 0; /* 0 - no starttag, 1 - '/' starttag, 2 - '=' starttag + '}' endtag */
173
174 if (!name) {
175 /* removing last path segment */
176 if (ctx->path[ctx->path_len - 1] == '}') {
177 for (; ctx->path[ctx->path_len] != '=' && ctx->path[ctx->path_len] != '{'; --ctx->path_len);
178 if (ctx->path[ctx->path_len] == '=') {
179 ctx->path[ctx->path_len++] = '}';
180 } else {
181 /* not a top-level special tag, remove also preceiding '/' */
182 goto remove_nodelevel;
183 }
184 } else {
185remove_nodelevel:
186 for (; ctx->path[ctx->path_len] != '/' ; --ctx->path_len);
187 if (ctx->path_len == 0) {
188 /* top-level (last segment) */
Radek Krejciacc79042019-07-25 14:14:57 +0200189 ctx->path_len = 1;
Radek Krejci327de162019-06-14 12:52:07 +0200190 }
191 }
192 /* set new terminating NULL-byte */
193 ctx->path[ctx->path_len] = '\0';
194 } else {
195 if (ctx->path_len > 1) {
196 if (!parent && ctx->path[ctx->path_len - 1] == '}' && ctx->path[ctx->path_len - 2] != '\'') {
197 /* extension of the special tag */
198 nextlevel = 2;
199 --ctx->path_len;
200 } else {
201 /* there is already some path, so add next level */
202 nextlevel = 1;
203 }
204 } /* else the path is just initiated with '/', so do not add additional slash in case of top-level nodes */
205
206 if (nextlevel != 2) {
207 if ((parent && parent->module == ctx->mod) || (!parent && ctx->path_len > 1 && name[0] == '{')) {
208 /* module not changed, print the name unprefixed */
Radek Krejci70ee9152019-07-25 11:27:27 +0200209 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "%s%s", nextlevel ? "/" : "", name);
Radek Krejci327de162019-06-14 12:52:07 +0200210 } else {
Radek Krejci70ee9152019-07-25 11:27:27 +0200211 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "%s%s:%s", nextlevel ? "/" : "", ctx->mod->name, name);
Radek Krejci327de162019-06-14 12:52:07 +0200212 }
213 } else {
Radek Krejci70ee9152019-07-25 11:27:27 +0200214 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "='%s'}", name);
Radek Krejci327de162019-06-14 12:52:07 +0200215 }
Radek Krejciacc79042019-07-25 14:14:57 +0200216 if (len >= LYSC_CTX_BUFSIZE - ctx->path_len) {
217 /* output truncated */
218 ctx->path_len = LYSC_CTX_BUFSIZE - 1;
219 } else {
220 ctx->path_len += len;
221 }
Radek Krejci327de162019-06-14 12:52:07 +0200222 }
223}
224
225/**
Radek Krejcib56c7502019-02-13 14:19:54 +0100226 * @brief Duplicate the compiled pattern structure.
227 *
228 * Instead of duplicating memory, the reference counter in the @p orig is increased.
229 *
230 * @param[in] orig The pattern structure to duplicate.
231 * @return The duplicated structure to use.
232 */
Radek Krejci19a96102018-11-15 13:38:09 +0100233static struct lysc_pattern*
234lysc_pattern_dup(struct lysc_pattern *orig)
235{
236 ++orig->refcount;
237 return orig;
238}
239
Radek Krejcib56c7502019-02-13 14:19:54 +0100240/**
241 * @brief Duplicate the array of compiled patterns.
242 *
243 * The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter.
244 *
245 * @param[in] ctx Libyang context for logging.
246 * @param[in] orig The patterns sized array to duplicate.
247 * @return New sized array as a copy of @p orig.
248 * @return NULL in case of memory allocation error.
249 */
Radek Krejci19a96102018-11-15 13:38:09 +0100250static struct lysc_pattern**
251lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig)
252{
Radek Krejcid05cbd92018-12-05 14:26:40 +0100253 struct lysc_pattern **dup = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100254 unsigned int u;
255
Radek Krejcib56c7502019-02-13 14:19:54 +0100256 assert(orig);
257
Radek Krejci19a96102018-11-15 13:38:09 +0100258 LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_SIZE(orig), NULL);
259 LY_ARRAY_FOR(orig, u) {
260 dup[u] = lysc_pattern_dup(orig[u]);
261 LY_ARRAY_INCREMENT(dup);
262 }
263 return dup;
264}
265
Radek Krejcib56c7502019-02-13 14:19:54 +0100266/**
267 * @brief Duplicate compiled range structure.
268 *
269 * @param[in] ctx Libyang context for logging.
270 * @param[in] orig The range structure to be duplicated.
271 * @return New compiled range structure as a copy of @p orig.
272 * @return NULL in case of memory allocation error.
273 */
Radek Krejci19a96102018-11-15 13:38:09 +0100274struct lysc_range*
275lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig)
276{
277 struct lysc_range *dup;
278 LY_ERR ret;
279
Radek Krejcib56c7502019-02-13 14:19:54 +0100280 assert(orig);
281
Radek Krejci19a96102018-11-15 13:38:09 +0100282 dup = calloc(1, sizeof *dup);
283 LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL);
284 if (orig->parts) {
285 LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_SIZE(orig->parts), ret, cleanup);
286 LY_ARRAY_SIZE(dup->parts) = LY_ARRAY_SIZE(orig->parts);
287 memcpy(dup->parts, orig->parts, LY_ARRAY_SIZE(dup->parts) * sizeof *dup->parts);
288 }
289 DUP_STRING(ctx, orig->eapptag, dup->eapptag);
290 DUP_STRING(ctx, orig->emsg, dup->emsg);
291 dup->exts = lysc_ext_instance_dup(ctx, orig->exts);
292
293 return dup;
294cleanup:
295 free(dup);
296 (void) ret; /* set but not used due to the return type */
297 return NULL;
298}
299
Radek Krejcib56c7502019-02-13 14:19:54 +0100300/**
301 * @brief Stack for processing if-feature expressions.
302 */
Radek Krejci19a96102018-11-15 13:38:09 +0100303struct iff_stack {
Radek Krejcib56c7502019-02-13 14:19:54 +0100304 int size; /**< number of items in the stack */
305 int index; /**< first empty item */
306 uint8_t *stack;/**< stack - array of @ref ifftokens to create the if-feature expression in prefix format */
Radek Krejci19a96102018-11-15 13:38:09 +0100307};
308
Radek Krejcib56c7502019-02-13 14:19:54 +0100309/**
310 * @brief Add @ref ifftokens into the stack.
311 * @param[in] stack The if-feature stack to use.
312 * @param[in] value One of the @ref ifftokens to store in the stack.
313 * @return LY_EMEM in case of memory allocation error
314 * @return LY_ESUCCESS if the value successfully stored.
315 */
Radek Krejci19a96102018-11-15 13:38:09 +0100316static LY_ERR
317iff_stack_push(struct iff_stack *stack, uint8_t value)
318{
319 if (stack->index == stack->size) {
320 stack->size += 4;
321 stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack);
322 LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM);
323 }
324 stack->stack[stack->index++] = value;
325 return LY_SUCCESS;
326}
327
Radek Krejcib56c7502019-02-13 14:19:54 +0100328/**
329 * @brief Get (and remove) the last item form the stack.
330 * @param[in] stack The if-feature stack to use.
331 * @return The value from the top of the stack.
332 */
Radek Krejci19a96102018-11-15 13:38:09 +0100333static uint8_t
334iff_stack_pop(struct iff_stack *stack)
335{
Radek Krejcib56c7502019-02-13 14:19:54 +0100336 assert(stack && stack->index);
337
Radek Krejci19a96102018-11-15 13:38:09 +0100338 stack->index--;
339 return stack->stack[stack->index];
340}
341
Radek Krejcib56c7502019-02-13 14:19:54 +0100342/**
343 * @brief Clean up the stack.
344 * @param[in] stack The if-feature stack to use.
345 */
Radek Krejci19a96102018-11-15 13:38:09 +0100346static void
347iff_stack_clean(struct iff_stack *stack)
348{
349 stack->size = 0;
350 free(stack->stack);
351}
352
Radek Krejcib56c7502019-02-13 14:19:54 +0100353/**
354 * @brief Store the @ref ifftokens (@p op) on the given position in the 2bits array
355 * (libyang format of the if-feature expression).
356 * @param[in,out] list The 2bits array to modify.
357 * @param[in] op The operand (@ref ifftokens) to store.
358 * @param[in] pos Position (0-based) where to store the given @p op.
359 */
Radek Krejci19a96102018-11-15 13:38:09 +0100360static void
361iff_setop(uint8_t *list, uint8_t op, int pos)
362{
363 uint8_t *item;
364 uint8_t mask = 3;
365
366 assert(pos >= 0);
367 assert(op <= 3); /* max 2 bits */
368
369 item = &list[pos / 4];
370 mask = mask << 2 * (pos % 4);
371 *item = (*item) & ~mask;
372 *item = (*item) | (op << 2 * (pos % 4));
373}
374
Radek Krejcib56c7502019-02-13 14:19:54 +0100375#define LYS_IFF_LP 0x04 /**< Additional, temporary, value of @ref ifftokens: ( */
376#define LYS_IFF_RP 0x08 /**< Additional, temporary, value of @ref ifftokens: ) */
Radek Krejci19a96102018-11-15 13:38:09 +0100377
Radek Krejci0af46292019-01-11 16:02:31 +0100378/**
379 * @brief Find a feature of the given name and referenced in the given module.
380 *
381 * If the compiled schema is available (the schema is implemented), the feature from the compiled schema is
382 * returned. Otherwise, the special array of pre-compiled features is used to search for the feature. Such
383 * features are always disabled (feature from not implemented schema cannot be enabled), but in case the schema
384 * will be made implemented in future (no matter if implicitly via augmenting/deviating it or explicitly via
385 * ly_ctx_module_implement()), the compilation of these feature structure is finished, but the pointers
386 * assigned till that time will be still valid.
387 *
388 * @param[in] mod Module where the feature was referenced (used to resolve prefix of the feature).
389 * @param[in] name Name of the feature including possible prefix.
390 * @param[in] len Length of the string representing the feature identifier in the name variable (mandatory!).
391 * @return Pointer to the feature structure if found, NULL otherwise.
392 */
Radek Krejci19a96102018-11-15 13:38:09 +0100393static struct lysc_feature *
Radek Krejci0af46292019-01-11 16:02:31 +0100394lys_feature_find(struct lys_module *mod, const char *name, size_t len)
Radek Krejci19a96102018-11-15 13:38:09 +0100395{
396 size_t i;
Radek Krejci0af46292019-01-11 16:02:31 +0100397 struct lysc_feature *f, *flist;
Radek Krejci19a96102018-11-15 13:38:09 +0100398
399 for (i = 0; i < len; ++i) {
400 if (name[i] == ':') {
401 /* we have a prefixed feature */
Radek Krejci0af46292019-01-11 16:02:31 +0100402 mod = lys_module_find_prefix(mod, name, i);
Radek Krejci19a96102018-11-15 13:38:09 +0100403 LY_CHECK_RET(!mod, NULL);
404
405 name = &name[i + 1];
406 len = len - i - 1;
407 }
408 }
409
410 /* we have the correct module, get the feature */
Radek Krejci0af46292019-01-11 16:02:31 +0100411 if (mod->implemented) {
412 /* module is implemented so there is already the compiled schema */
413 flist = mod->compiled->features;
414 } else {
415 flist = mod->off_features;
416 }
417 LY_ARRAY_FOR(flist, i) {
418 f = &flist[i];
Radek Krejci7f9b6512019-09-18 13:11:09 +0200419 if (!ly_strncmp(f->name, name, len)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100420 return f;
421 }
422 }
423
424 return NULL;
425}
426
427static LY_ERR
Radek Krejci0935f412019-08-20 16:15:18 +0200428lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext, void *parent, LYEXT_PARENT parent_type)
Radek Krejci19a96102018-11-15 13:38:09 +0100429{
Radek Krejci7c960162019-09-18 14:16:12 +0200430 LY_ERR ret = LY_EVALID;
Radek Krejci19a96102018-11-15 13:38:09 +0100431 const char *name;
432 unsigned int u;
433 const struct lys_module *mod;
Radek Krejci0935f412019-08-20 16:15:18 +0200434 struct lysc_ext *elist = NULL;
Radek Krejci7c960162019-09-18 14:16:12 +0200435 const char *prefixed_name = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100436
437 DUP_STRING(ctx->ctx, ext_p->argument, ext->argument);
438 ext->insubstmt = ext_p->insubstmt;
439 ext->insubstmt_index = ext_p->insubstmt_index;
Radek Krejci28681fa2019-09-06 13:08:45 +0200440 ext->module = ctx->mod_def;
Radek Krejci0935f412019-08-20 16:15:18 +0200441 ext->parent = parent;
442 ext->parent_type = parent_type;
Radek Krejci19a96102018-11-15 13:38:09 +0100443
Radek Krejcif56e2a42019-09-09 14:15:25 +0200444 lysc_update_path(ctx, ext->parent_type == LYEXT_PAR_NODE ? (struct lysc_node*)ext->parent : NULL, "{extension}");
Radek Krejcif56e2a42019-09-09 14:15:25 +0200445
Radek Krejci19a96102018-11-15 13:38:09 +0100446 /* get module where the extension definition should be placed */
Radek Krejci7c960162019-09-18 14:16:12 +0200447 for (u = strlen(ext_p->name); u && ext_p->name[u - 1] != ':'; --u);
448 if (ext_p->yin) {
449 /* YIN parser has to replace prefixes by the namespace - XML namespace/prefix pairs may differs form the YANG schema's
450 * namespace/prefix pair. YIN parser does not have the imports available, so mapping from XML namespace to the
451 * YANG (import) prefix must be done here. */
452 if (!ly_strncmp(ctx->mod_def->ns, ext_p->name, u - 1)) {
453 prefixed_name = lydict_insert(ctx->ctx, &ext_p->name[u], 0);
454 u = 0;
455 } else if (ctx->mod_def->compiled) {
456 unsigned int v;
457 LY_ARRAY_FOR(ctx->mod_def->compiled->imports, v) {
458 if (!ly_strncmp(ctx->mod_def->compiled->imports[v].module->ns, ext_p->name, u - 1)) {
459 char *s;
460 asprintf(&s, "%s:%s", ctx->mod_def->compiled->imports[v].prefix, &ext_p->name[u]);
461 prefixed_name = lydict_insert_zc(ctx->ctx, s);
462 u = strlen(ctx->mod_def->compiled->imports[v].prefix) + 1; /* add semicolon */
463 break;
464 }
465 }
466 }
467 if (!prefixed_name) {
468 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
469 "Invalid XML prefix of \"%.*s\" namespace used for extension instance identifier.", u, ext_p->name);
470 goto cleanup;
471 }
472 } else {
473 prefixed_name = ext_p->name;
474 }
475 lysc_update_path(ctx, NULL, prefixed_name);
476
477 mod = lys_module_find_prefix(ctx->mod_def, prefixed_name, u - 1);
478 if (!mod) {
479 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
480 "Invalid prefix \"%.*s\" used for extension instance identifier.", u, prefixed_name);
481 goto cleanup;
482 } else if (!mod->parsed->extensions) {
483 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
484 "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.",
485 prefixed_name, mod->name);
486 goto cleanup;
487 }
488 name = &prefixed_name[u];
Radek Krejci0935f412019-08-20 16:15:18 +0200489
Radek Krejci19a96102018-11-15 13:38:09 +0100490 /* find the extension definition there */
Radek Krejci0935f412019-08-20 16:15:18 +0200491 if (mod->off_extensions) {
492 elist = mod->off_extensions;
493 } else {
494 elist = mod->compiled->extensions;
495 }
496 LY_ARRAY_FOR(elist, u) {
497 if (!strcmp(name, elist[u].name)) {
498 ext->def = &elist[u];
Radek Krejci19a96102018-11-15 13:38:09 +0100499 break;
500 }
501 }
Radek Krejci7c960162019-09-18 14:16:12 +0200502 if (!ext->def) {
503 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
504 "Extension definition of extension instance \"%s\" not found.", prefixed_name);
505 goto cleanup;
506 }
Radek Krejci0935f412019-08-20 16:15:18 +0200507
Radek Krejcif56e2a42019-09-09 14:15:25 +0200508 /* unify the parsed extension from YIN and YANG sources. Without extension definition, it is not possible
509 * to get extension's argument from YIN source, so it is stored as one of the substatements. Here we have
510 * to find it, mark it with LYS_YIN_ARGUMENT and store it in the compiled structure. */
Radek Krejci7c960162019-09-18 14:16:12 +0200511 if (ext_p->yin && ext->def->argument && !ext->argument) {
Radek Krejcif56e2a42019-09-09 14:15:25 +0200512 /* Schema was parsed from YIN and an argument is expected, ... */
513 struct lysp_stmt *stmt = NULL;
514
515 if (ext->def->flags & LYS_YINELEM_TRUE) {
516 /* ... argument was the first XML child element */
517 if (ext_p->child && !(ext_p->child->flags & LYS_YIN_ATTR)) {
518 /* TODO check namespace of the statement */
519 if (!strcmp(ext_p->child->stmt, ext->def->argument)) {
520 stmt = ext_p->child;
521 }
522 }
523 } else {
524 /* ... argument was one of the XML attributes which are represented as child stmt
525 * with LYS_YIN_ATTR flag */
526 for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) {
527 if (!strcmp(stmt->stmt, ext->def->argument)) {
528 /* this is the extension's argument */
Radek Krejcif56e2a42019-09-09 14:15:25 +0200529 break;
530 }
531 }
532 }
533 if (!stmt) {
534 /* missing extension's argument */
535 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci7c960162019-09-18 14:16:12 +0200536 "Extension instance \"%s\" misses argument \"%s\".", prefixed_name, ext->def->argument);
537 goto cleanup;
Radek Krejcif56e2a42019-09-09 14:15:25 +0200538
539 }
540 ext->argument = lydict_insert(ctx->ctx, stmt->arg, 0);
541 stmt->flags |= LYS_YIN_ARGUMENT;
542 }
Radek Krejci7c960162019-09-18 14:16:12 +0200543 if (prefixed_name != ext_p->name) {
544 lydict_remove(ctx->ctx, ext_p->name);
545 ext_p->name = prefixed_name;
546 if (!ext_p->argument && ext->argument) {
547 ext_p->argument = lydict_insert(ctx->ctx, ext->argument, 0);
548 }
549 }
Radek Krejcif56e2a42019-09-09 14:15:25 +0200550
Radek Krejci0935f412019-08-20 16:15:18 +0200551 if (ext->def->plugin && ext->def->plugin->compile) {
Radek Krejciad5963b2019-09-06 16:03:05 +0200552 if (ext->argument) {
553 lysc_update_path(ctx, (struct lysc_node*)ext, ext->argument);
554 }
Radek Krejci7c960162019-09-18 14:16:12 +0200555 LY_CHECK_GOTO(ext->def->plugin->compile(ctx, ext_p, ext), cleanup);
Radek Krejciad5963b2019-09-06 16:03:05 +0200556 if (ext->argument) {
557 lysc_update_path(ctx, NULL, NULL);
558 }
Radek Krejci0935f412019-08-20 16:15:18 +0200559 }
Radek Krejcif56e2a42019-09-09 14:15:25 +0200560 ext_p->compiled = ext;
561
Radek Krejci7c960162019-09-18 14:16:12 +0200562 ret = LY_SUCCESS;
563cleanup:
564 if (prefixed_name && prefixed_name != ext_p->name) {
565 lydict_remove(ctx->ctx, prefixed_name);
566 }
567
Radek Krejcif56e2a42019-09-09 14:15:25 +0200568 lysc_update_path(ctx, NULL, NULL);
569 lysc_update_path(ctx, NULL, NULL);
Radek Krejci0935f412019-08-20 16:15:18 +0200570
Radek Krejci7c960162019-09-18 14:16:12 +0200571 return ret;
Radek Krejci0935f412019-08-20 16:15:18 +0200572}
573
574/**
575 * @brief Fill in the prepared compiled extensions definition structure according to the parsed extension definition.
576 */
577static LY_ERR
578lys_compile_extension(struct lysc_ctx *ctx, struct lysp_ext *ext_p, struct lysc_ext *ext)
579{
580 LY_ERR ret = LY_SUCCESS;
581
582 DUP_STRING(ctx->ctx, ext_p->name, ext->name);
583 DUP_STRING(ctx->ctx, ext_p->argument, ext->argument);
584 ext->module = ctx->mod_def;
585 COMPILE_EXTS_GOTO(ctx, ext_p->exts, ext->exts, ext, LYEXT_PAR_EXT, ret, done);
586
587done:
588 return ret;
589}
590
591/**
592 * @brief Link the extensions definitions with the available extension plugins.
593 *
594 * This is done only in the compiled (implemented) module. Extensions of a non-implemented modules
595 * are not connected with even available extension plugins.
596 *
597 * @param[in] extensions List of extensions to be processed ([sized array](@ref sizedarrays)).
598 */
599static void
600lys_compile_extension_plugins(struct lysc_ext *extensions)
601{
602 unsigned int u;
603
604 LY_ARRAY_FOR(extensions, u) {
605 extensions[u].plugin = lyext_get_plugin(&extensions[u]);
606 }
607}
608
609LY_ERR
610lys_extension_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module,
611 struct lysp_ext *extensions_p, struct lysc_ext **extensions)
612{
613 unsigned int offset = 0, u;
614 struct lysc_ctx context = {0};
615
616 assert(ctx_sc || ctx);
617
618 if (!ctx_sc) {
619 context.ctx = ctx;
620 context.mod = module;
621 context.path_len = 1;
622 context.path[0] = '/';
623 ctx_sc = &context;
624 }
625
626 if (!extensions_p) {
627 return LY_SUCCESS;
628 }
629 if (*extensions) {
630 offset = LY_ARRAY_SIZE(*extensions);
631 }
632
633 lysc_update_path(ctx_sc, NULL, "{extension}");
634 LY_ARRAY_CREATE_RET(ctx_sc->ctx, *extensions, LY_ARRAY_SIZE(extensions_p), LY_EMEM);
635 LY_ARRAY_FOR(extensions_p, u) {
636 lysc_update_path(ctx_sc, NULL, extensions_p[u].name);
637 LY_ARRAY_INCREMENT(*extensions);
638 COMPILE_CHECK_UNIQUENESS(ctx_sc, *extensions, name, &(*extensions)[offset + u], "extension", extensions_p[u].name);
639 LY_CHECK_RET(lys_compile_extension(ctx_sc, &extensions_p[u], &(*extensions)[offset + u]));
640 lysc_update_path(ctx_sc, NULL, NULL);
641 }
642 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +0100643
644 return LY_SUCCESS;
645}
646
Radek Krejcib56c7502019-02-13 14:19:54 +0100647/**
648 * @brief Compile information from the if-feature statement
649 * @param[in] ctx Compile context.
650 * @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 +0100651 * @param[in,out] iff Prepared (empty) compiled if-feature structure to fill.
652 * @return LY_ERR value.
653 */
Radek Krejci19a96102018-11-15 13:38:09 +0100654static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200655lys_compile_iffeature(struct lysc_ctx *ctx, const char **value, struct lysc_iffeature *iff)
Radek Krejci19a96102018-11-15 13:38:09 +0100656{
657 const char *c = *value;
658 int r, rc = EXIT_FAILURE;
659 int i, j, last_not, checkversion = 0;
660 unsigned int f_size = 0, expr_size = 0, f_exp = 1;
661 uint8_t op;
662 struct iff_stack stack = {0, 0, NULL};
663 struct lysc_feature *f;
664
665 assert(c);
666
667 /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */
668 for (i = j = last_not = 0; c[i]; i++) {
669 if (c[i] == '(') {
670 j++;
671 checkversion = 1;
672 continue;
673 } else if (c[i] == ')') {
674 j--;
675 continue;
676 } else if (isspace(c[i])) {
677 checkversion = 1;
678 continue;
679 }
680
681 if (!strncmp(&c[i], "not", r = 3) || !strncmp(&c[i], "and", r = 3) || !strncmp(&c[i], "or", r = 2)) {
Radek Krejci6788abc2019-06-14 13:56:49 +0200682 int sp;
683 for(sp = 0; c[i + r + sp] && isspace(c[i + r + sp]); sp++);
684 if (c[i + r + sp] == '\0') {
Radek Krejci19a96102018-11-15 13:38:09 +0100685 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
686 "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value);
687 return LY_EVALID;
688 } else if (!isspace(c[i + r])) {
689 /* feature name starting with the not/and/or */
690 last_not = 0;
691 f_size++;
692 } else if (c[i] == 'n') { /* not operation */
693 if (last_not) {
694 /* double not */
695 expr_size = expr_size - 2;
696 last_not = 0;
697 } else {
698 last_not = 1;
699 }
700 } else { /* and, or */
Radek Krejci6788abc2019-06-14 13:56:49 +0200701 if (f_exp != f_size) {
702 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
703 "Invalid value \"%s\" of if-feature - missing feature/expression before \"%.*s\" operation.", *value, r, &c[i]);
704 return LY_EVALID;
705 }
Radek Krejci19a96102018-11-15 13:38:09 +0100706 f_exp++;
Radek Krejci6788abc2019-06-14 13:56:49 +0200707
Radek Krejci19a96102018-11-15 13:38:09 +0100708 /* not a not operation */
709 last_not = 0;
710 }
711 i += r;
712 } else {
713 f_size++;
714 last_not = 0;
715 }
716 expr_size++;
717
718 while (!isspace(c[i])) {
Radek Krejci6788abc2019-06-14 13:56:49 +0200719 if (!c[i] || c[i] == ')' || c[i] == '(') {
Radek Krejci19a96102018-11-15 13:38:09 +0100720 i--;
721 break;
722 }
723 i++;
724 }
725 }
Radek Krejci6788abc2019-06-14 13:56:49 +0200726 if (j) {
Radek Krejci19a96102018-11-15 13:38:09 +0100727 /* not matching count of ( and ) */
728 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
729 "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value);
730 return LY_EVALID;
731 }
Radek Krejci6788abc2019-06-14 13:56:49 +0200732 if (f_exp != f_size) {
733 /* features do not match the needed arguments for the logical operations */
734 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
735 "Invalid value \"%s\" of if-feature - number of features in expression does not match "
736 "the required number of operands for the operations.", *value);
737 return LY_EVALID;
738 }
Radek Krejci19a96102018-11-15 13:38:09 +0100739
740 if (checkversion || expr_size > 1) {
741 /* check that we have 1.1 module */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100742 if (ctx->mod_def->version != LYS_VERSION_1_1) {
Radek Krejci19a96102018-11-15 13:38:09 +0100743 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
744 "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value);
745 return LY_EVALID;
746 }
747 }
748
749 /* allocate the memory */
750 LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM);
751 iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr);
752 stack.stack = malloc(expr_size * sizeof *stack.stack);
753 LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx), error);
754
755 stack.size = expr_size;
756 f_size--; expr_size--; /* used as indexes from now */
757
758 for (i--; i >= 0; i--) {
759 if (c[i] == ')') {
760 /* push it on stack */
761 iff_stack_push(&stack, LYS_IFF_RP);
762 continue;
763 } else if (c[i] == '(') {
764 /* pop from the stack into result all operators until ) */
765 while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) {
766 iff_setop(iff->expr, op, expr_size--);
767 }
768 continue;
769 } else if (isspace(c[i])) {
770 continue;
771 }
772
773 /* end of operator or operand -> find beginning and get what is it */
774 j = i + 1;
775 while (i >= 0 && !isspace(c[i]) && c[i] != '(') {
776 i--;
777 }
778 i++; /* go back by one step */
779
780 if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) {
781 if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) {
782 /* double not */
783 iff_stack_pop(&stack);
784 } else {
785 /* not has the highest priority, so do not pop from the stack
786 * as in case of AND and OR */
787 iff_stack_push(&stack, LYS_IFF_NOT);
788 }
789 } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) {
790 /* as for OR - pop from the stack all operators with the same or higher
791 * priority and store them to the result, then push the AND to the stack */
792 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) {
793 op = iff_stack_pop(&stack);
794 iff_setop(iff->expr, op, expr_size--);
795 }
796 iff_stack_push(&stack, LYS_IFF_AND);
797 } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) {
798 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) {
799 op = iff_stack_pop(&stack);
800 iff_setop(iff->expr, op, expr_size--);
801 }
802 iff_stack_push(&stack, LYS_IFF_OR);
803 } else {
804 /* feature name, length is j - i */
805
806 /* add it to the expression */
807 iff_setop(iff->expr, LYS_IFF_F, expr_size--);
808
809 /* now get the link to the feature definition */
Radek Krejci0af46292019-01-11 16:02:31 +0100810 f = lys_feature_find(ctx->mod_def, &c[i], j - i);
811 LY_CHECK_ERR_GOTO(!f, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
812 "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]);
813 rc = LY_EVALID, error)
Radek Krejci19a96102018-11-15 13:38:09 +0100814 iff->features[f_size] = f;
815 LY_ARRAY_INCREMENT(iff->features);
816 f_size--;
817 }
818 }
819 while (stack.index) {
820 op = iff_stack_pop(&stack);
821 iff_setop(iff->expr, op, expr_size--);
822 }
823
824 if (++expr_size || ++f_size) {
825 /* not all expected operators and operands found */
826 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
827 "Invalid value \"%s\" of if-feature - processing error.", *value);
828 rc = LY_EINT;
829 } else {
830 rc = LY_SUCCESS;
831 }
832
833error:
834 /* cleanup */
835 iff_stack_clean(&stack);
836
837 return rc;
838}
839
Radek Krejcib56c7502019-02-13 14:19:54 +0100840/**
Michal Vasko175012e2019-11-06 15:49:14 +0100841 * @brief Get the XPath context node for the given schema node.
842 * @param[in] start The schema node where the XPath expression appears.
843 * @return The context node to evaluate XPath expression in given schema node.
844 * @return NULL in case the context node is the root node.
845 */
846static struct lysc_node *
847lysc_xpath_context(struct lysc_node *start)
848{
849 for (; start && !(start->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_ACTION | LYS_NOTIF));
850 start = start->parent);
851 return start;
852}
853
854/**
Radek Krejcib56c7502019-02-13 14:19:54 +0100855 * @brief Compile information from the when statement
856 * @param[in] ctx Compile context.
857 * @param[in] when_p The parsed when statement structure.
Michal Vasko175012e2019-11-06 15:49:14 +0100858 * @param[in] flags Flags of the node with the "when" defiition.
859 * @param[in] node Node that inherited the "when" definition, must be connected to parents.
Radek Krejcib56c7502019-02-13 14:19:54 +0100860 * @param[out] when Pointer where to store pointer to the created compiled when structure.
861 * @return LY_ERR value.
862 */
Radek Krejci19a96102018-11-15 13:38:09 +0100863static LY_ERR
Michal Vasko175012e2019-11-06 15:49:14 +0100864lys_compile_when(struct lysc_ctx *ctx, struct lysp_when *when_p, uint16_t flags, struct lysc_node *node, struct lysc_when **when)
Radek Krejci19a96102018-11-15 13:38:09 +0100865{
Radek Krejci19a96102018-11-15 13:38:09 +0100866 LY_ERR ret = LY_SUCCESS;
867
Radek Krejci00b874b2019-02-12 10:54:50 +0100868 *when = calloc(1, sizeof **when);
869 (*when)->refcount = 1;
870 (*when)->cond = lyxp_expr_parse(ctx->ctx, when_p->cond);
Radek Krejcia0f704a2019-09-09 16:12:23 +0200871 (*when)->module = ctx->mod_def;
Michal Vasko175012e2019-11-06 15:49:14 +0100872 (*when)->context = lysc_xpath_context(node);
Radek Krejci00b874b2019-02-12 10:54:50 +0100873 DUP_STRING(ctx->ctx, when_p->dsc, (*when)->dsc);
874 DUP_STRING(ctx->ctx, when_p->ref, (*when)->ref);
875 LY_CHECK_ERR_GOTO(!(*when)->cond, ret = ly_errcode(ctx->ctx), done);
Radek Krejci0935f412019-08-20 16:15:18 +0200876 COMPILE_EXTS_GOTO(ctx, when_p->exts, (*when)->exts, (*when), LYEXT_PAR_WHEN, ret, done);
Michal Vasko175012e2019-11-06 15:49:14 +0100877 (*when)->flags = flags & LYS_STATUS_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +0100878
879done:
880 return ret;
881}
882
Radek Krejcib56c7502019-02-13 14:19:54 +0100883/**
884 * @brief Compile information from the must statement
885 * @param[in] ctx Compile context.
886 * @param[in] must_p The parsed must statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100887 * @param[in,out] must Prepared (empty) compiled must structure to fill.
888 * @return LY_ERR value.
889 */
Radek Krejci19a96102018-11-15 13:38:09 +0100890static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200891lys_compile_must(struct lysc_ctx *ctx, struct lysp_restr *must_p, struct lysc_must *must)
Radek Krejci19a96102018-11-15 13:38:09 +0100892{
Radek Krejci19a96102018-11-15 13:38:09 +0100893 LY_ERR ret = LY_SUCCESS;
894
895 must->cond = lyxp_expr_parse(ctx->ctx, must_p->arg);
896 LY_CHECK_ERR_GOTO(!must->cond, ret = ly_errcode(ctx->ctx), done);
Radek Krejci462cf252019-07-24 09:49:08 +0200897 must->module = ctx->mod_def;
Radek Krejci19a96102018-11-15 13:38:09 +0100898 DUP_STRING(ctx->ctx, must_p->eapptag, must->eapptag);
899 DUP_STRING(ctx->ctx, must_p->emsg, must->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +0100900 DUP_STRING(ctx->ctx, must_p->dsc, must->dsc);
901 DUP_STRING(ctx->ctx, must_p->ref, must->ref);
Radek Krejci0935f412019-08-20 16:15:18 +0200902 COMPILE_EXTS_GOTO(ctx, must_p->exts, must->exts, must, LYEXT_PAR_MUST, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100903
904done:
905 return ret;
906}
907
Radek Krejcib56c7502019-02-13 14:19:54 +0100908/**
909 * @brief Compile information from the import statement
910 * @param[in] ctx Compile context.
911 * @param[in] imp_p The parsed import statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100912 * @param[in,out] imp Prepared (empty) compiled import structure to fill.
913 * @return LY_ERR value.
914 */
Radek Krejci19a96102018-11-15 13:38:09 +0100915static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200916lys_compile_import(struct lysc_ctx *ctx, struct lysp_import *imp_p, struct lysc_import *imp)
Radek Krejci19a96102018-11-15 13:38:09 +0100917{
Radek Krejci19a96102018-11-15 13:38:09 +0100918 struct lys_module *mod = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100919 LY_ERR ret = LY_SUCCESS;
920
921 DUP_STRING(ctx->ctx, imp_p->prefix, imp->prefix);
Radek Krejci0935f412019-08-20 16:15:18 +0200922 COMPILE_EXTS_GOTO(ctx, imp_p->exts, imp->exts, imp, LYEXT_PAR_IMPORT, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100923 imp->module = imp_p->module;
924
Radek Krejci7f2a5362018-11-28 13:05:37 +0100925 /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs.
926 * The compiled version is needed only for augments, deviates and leafrefs, so they are checked (and added,
Radek Krejci0e5d8382018-11-28 16:37:53 +0100927 * if needed) when these nodes are finally being instantiated and validated at the end of schema compilation. */
Radek Krejci19a96102018-11-15 13:38:09 +0100928 if (!imp->module->parsed) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100929 /* try to use filepath if present */
930 if (imp->module->filepath) {
931 mod = (struct lys_module*)lys_parse_path(ctx->ctx, imp->module->filepath,
932 !strcmp(&imp->module->filepath[strlen(imp->module->filepath - 4)], ".yin") ? LYS_IN_YIN : LYS_IN_YANG);
Radek Krejci19a96102018-11-15 13:38:09 +0100933 if (mod != imp->module) {
934 LOGERR(ctx->ctx, LY_EINT, "Filepath \"%s\" of the module \"%s\" does not match.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100935 imp->module->filepath, imp->module->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100936 mod = NULL;
937 }
938 }
939 if (!mod) {
Radek Krejci0af46292019-01-11 16:02:31 +0100940 if (lysp_load_module(ctx->ctx, imp->module->name, imp->module->revision, 0, 1, &mod)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100941 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 +0100942 imp->module->name, ctx->mod->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100943 return LY_ENOTFOUND;
944 }
945 }
Radek Krejci19a96102018-11-15 13:38:09 +0100946 }
947
948done:
949 return ret;
950}
951
Radek Krejcib56c7502019-02-13 14:19:54 +0100952/**
953 * @brief Compile information from the identity statement
954 *
955 * The backlinks to the identities derived from this one are supposed to be filled later via lys_compile_identity_bases().
956 *
957 * @param[in] ctx Compile context.
958 * @param[in] ident_p The parsed identity statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100959 * @param[in] idents List of so far compiled identities to check the name uniqueness.
960 * @param[in,out] ident Prepared (empty) compiled identity structure to fill.
961 * @return LY_ERR value.
962 */
Radek Krejci19a96102018-11-15 13:38:09 +0100963static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200964lys_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 +0100965{
966 unsigned int u;
967 LY_ERR ret = LY_SUCCESS;
968
Radek Krejci327de162019-06-14 12:52:07 +0200969 lysc_update_path(ctx, NULL, ident_p->name);
970
Radek Krejcid05cbd92018-12-05 14:26:40 +0100971 COMPILE_CHECK_UNIQUENESS(ctx, idents, name, ident, "identity", ident_p->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100972 DUP_STRING(ctx->ctx, ident_p->name, ident->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200973 DUP_STRING(ctx->ctx, ident_p->dsc, ident->dsc);
974 DUP_STRING(ctx->ctx, ident_p->ref, ident->ref);
Radek Krejci693262f2019-04-29 15:23:20 +0200975 ident->module = ctx->mod;
Radek Krejciec4da802019-05-02 13:02:41 +0200976 COMPILE_ARRAY_GOTO(ctx, ident_p->iffeatures, ident->iffeatures, u, lys_compile_iffeature, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100977 /* backlings (derived) can be added no sooner than when all the identities in the current module are present */
Radek Krejci0935f412019-08-20 16:15:18 +0200978 COMPILE_EXTS_GOTO(ctx, ident_p->exts, ident->exts, ident, LYEXT_PAR_IDENT, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100979 ident->flags = ident_p->flags;
980
Radek Krejci327de162019-06-14 12:52:07 +0200981 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +0100982done:
983 return ret;
984}
985
Radek Krejcib56c7502019-02-13 14:19:54 +0100986/**
987 * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement).
988 *
989 * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages.
990 *
991 * @param[in] ctx Compile context for logging.
992 * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed).
993 * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident)
994 * @return LY_SUCCESS if everything is ok.
995 * @return LY_EVALID if the identity is derived from itself.
996 */
Radek Krejci38222632019-02-12 16:55:05 +0100997static LY_ERR
998lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived)
999{
1000 LY_ERR ret = LY_EVALID;
1001 unsigned int u, v;
1002 struct ly_set recursion = {0};
1003 struct lysc_ident *drv;
1004
1005 if (!derived) {
1006 return LY_SUCCESS;
1007 }
1008
1009 for (u = 0; u < LY_ARRAY_SIZE(derived); ++u) {
1010 if (ident == derived[u]) {
1011 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1012 "Identity \"%s\" is indirectly derived from itself.", ident->name);
1013 goto cleanup;
1014 }
1015 ly_set_add(&recursion, derived[u], 0);
1016 }
1017
1018 for (v = 0; v < recursion.count; ++v) {
1019 drv = recursion.objs[v];
1020 if (!drv->derived) {
1021 continue;
1022 }
1023 for (u = 0; u < LY_ARRAY_SIZE(drv->derived); ++u) {
1024 if (ident == drv->derived[u]) {
1025 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1026 "Identity \"%s\" is indirectly derived from itself.", ident->name);
1027 goto cleanup;
1028 }
1029 ly_set_add(&recursion, drv->derived[u], 0);
1030 }
1031 }
1032 ret = LY_SUCCESS;
1033
1034cleanup:
1035 ly_set_erase(&recursion, NULL);
1036 return ret;
1037}
1038
Radek Krejcia3045382018-11-22 14:30:31 +01001039/**
1040 * @brief Find and process the referenced base identities from another identity or identityref
1041 *
Radek Krejciaca74032019-06-04 08:53:06 +02001042 * For bases in identity set backlinks to them from the base identities. For identityref, store
Radek Krejcia3045382018-11-22 14:30:31 +01001043 * the array of pointers to the base identities. So one of the ident or bases parameter must be set
1044 * to distinguish these two use cases.
1045 *
1046 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
1047 * @param[in] bases_p Array of names (including prefix if necessary) of base identities.
1048 * @param[in] ident Referencing identity to work with.
1049 * @param[in] bases Array of bases of identityref to fill in.
1050 * @return LY_ERR value.
1051 */
Radek Krejci19a96102018-11-15 13:38:09 +01001052static LY_ERR
Radek Krejci555cb5b2018-11-16 14:54:33 +01001053lys_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 +01001054{
Radek Krejci555cb5b2018-11-16 14:54:33 +01001055 unsigned int u, v;
Radek Krejci19a96102018-11-15 13:38:09 +01001056 const char *s, *name;
Radek Krejcie86bf772018-12-14 11:39:53 +01001057 struct lys_module *mod;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001058 struct lysc_ident **idref;
1059
1060 assert(ident || bases);
1061
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001062 if (LY_ARRAY_SIZE(bases_p) > 1 && ctx->mod_def->version < 2) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01001063 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1064 "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type");
1065 return LY_EVALID;
1066 }
1067
1068 for (u = 0; u < LY_ARRAY_SIZE(bases_p); ++u) {
1069 s = strchr(bases_p[u], ':');
1070 if (s) {
1071 /* prefixed identity */
1072 name = &s[1];
Radek Krejcie86bf772018-12-14 11:39:53 +01001073 mod = lys_module_find_prefix(ctx->mod_def, bases_p[u], s - bases_p[u]);
Radek Krejci555cb5b2018-11-16 14:54:33 +01001074 } else {
1075 name = bases_p[u];
Radek Krejcie86bf772018-12-14 11:39:53 +01001076 mod = ctx->mod_def;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001077 }
1078 if (!mod) {
1079 if (ident) {
1080 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1081 "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name);
1082 } else {
1083 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1084 "Invalid prefix used for base (%s) of identityref.", bases_p[u]);
1085 }
1086 return LY_EVALID;
1087 }
1088 idref = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01001089 if (mod->compiled && mod->compiled->identities) {
1090 for (v = 0; v < LY_ARRAY_SIZE(mod->compiled->identities); ++v) {
1091 if (!strcmp(name, mod->compiled->identities[v].name)) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01001092 if (ident) {
Radek Krejci38222632019-02-12 16:55:05 +01001093 if (ident == &mod->compiled->identities[v]) {
1094 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1095 "Identity \"%s\" is derived from itself.", ident->name);
1096 return LY_EVALID;
1097 }
1098 LY_CHECK_RET(lys_compile_identity_circular_check(ctx, &mod->compiled->identities[v], ident->derived));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001099 /* we have match! store the backlink */
Radek Krejcie86bf772018-12-14 11:39:53 +01001100 LY_ARRAY_NEW_RET(ctx->ctx, mod->compiled->identities[v].derived, idref, LY_EMEM);
Radek Krejci555cb5b2018-11-16 14:54:33 +01001101 *idref = ident;
1102 } else {
1103 /* we have match! store the found identity */
1104 LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +01001105 *idref = &mod->compiled->identities[v];
Radek Krejci555cb5b2018-11-16 14:54:33 +01001106 }
1107 break;
1108 }
1109 }
1110 }
1111 if (!idref || !(*idref)) {
1112 if (ident) {
1113 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1114 "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name);
1115 } else {
1116 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1117 "Unable to find base (%s) of identityref.", bases_p[u]);
1118 }
1119 return LY_EVALID;
1120 }
1121 }
1122 return LY_SUCCESS;
1123}
1124
Radek Krejcia3045382018-11-22 14:30:31 +01001125/**
1126 * @brief For the given array of identities, set the backlinks from all their base identities.
1127 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
1128 * @param[in] idents_p Array of identities definitions from the parsed schema structure.
1129 * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set.
1130 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1131 */
Radek Krejci555cb5b2018-11-16 14:54:33 +01001132static LY_ERR
1133lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents)
1134{
1135 unsigned int i;
Radek Krejci19a96102018-11-15 13:38:09 +01001136
1137 for (i = 0; i < LY_ARRAY_SIZE(idents_p); ++i) {
1138 if (!idents_p[i].bases) {
1139 continue;
1140 }
Radek Krejci327de162019-06-14 12:52:07 +02001141 lysc_update_path(ctx, NULL, idents[i].name);
Radek Krejci555cb5b2018-11-16 14:54:33 +01001142 LY_CHECK_RET(lys_compile_identity_bases(ctx, idents_p[i].bases, &idents[i], NULL));
Radek Krejci327de162019-06-14 12:52:07 +02001143 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01001144 }
1145 return LY_SUCCESS;
1146}
1147
Radek Krejci0af46292019-01-11 16:02:31 +01001148LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02001149lys_feature_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module, struct lysp_feature *features_p, struct lysc_feature **features)
Radek Krejci0af46292019-01-11 16:02:31 +01001150{
1151 unsigned int offset = 0, u;
1152 struct lysc_ctx context = {0};
1153
Radek Krejci327de162019-06-14 12:52:07 +02001154 assert(ctx_sc || ctx);
1155
1156 if (!ctx_sc) {
1157 context.ctx = ctx;
1158 context.mod = module;
1159 context.path_len = 1;
1160 context.path[0] = '/';
1161 ctx_sc = &context;
1162 }
Radek Krejci0af46292019-01-11 16:02:31 +01001163
1164 if (!features_p) {
1165 return LY_SUCCESS;
1166 }
1167 if (*features) {
1168 offset = LY_ARRAY_SIZE(*features);
1169 }
1170
Radek Krejci327de162019-06-14 12:52:07 +02001171 lysc_update_path(ctx_sc, NULL, "{feature}");
1172 LY_ARRAY_CREATE_RET(ctx_sc->ctx, *features, LY_ARRAY_SIZE(features_p), LY_EMEM);
Radek Krejci0af46292019-01-11 16:02:31 +01001173 LY_ARRAY_FOR(features_p, u) {
Radek Krejci327de162019-06-14 12:52:07 +02001174 lysc_update_path(ctx_sc, NULL, features_p[u].name);
1175
Radek Krejci0af46292019-01-11 16:02:31 +01001176 LY_ARRAY_INCREMENT(*features);
Radek Krejci327de162019-06-14 12:52:07 +02001177 COMPILE_CHECK_UNIQUENESS(ctx_sc, *features, name, &(*features)[offset + u], "feature", features_p[u].name);
1178 DUP_STRING(ctx_sc->ctx, features_p[u].name, (*features)[offset + u].name);
1179 DUP_STRING(ctx_sc->ctx, features_p[u].dsc, (*features)[offset + u].dsc);
1180 DUP_STRING(ctx_sc->ctx, features_p[u].ref, (*features)[offset + u].ref);
Radek Krejci0af46292019-01-11 16:02:31 +01001181 (*features)[offset + u].flags = features_p[u].flags;
Radek Krejci327de162019-06-14 12:52:07 +02001182 (*features)[offset + u].module = ctx_sc->mod;
1183
1184 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001185 }
Radek Krejci327de162019-06-14 12:52:07 +02001186 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001187
1188 return LY_SUCCESS;
1189}
1190
Radek Krejcia3045382018-11-22 14:30:31 +01001191/**
Radek Krejci09a1fc52019-02-13 10:55:17 +01001192 * @brief Check circular dependency of features - feature MUST NOT reference itself (via their if-feature statement).
Radek Krejcib56c7502019-02-13 14:19:54 +01001193 *
1194 * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages.
1195 *
Radek Krejci09a1fc52019-02-13 10:55:17 +01001196 * @param[in] ctx Compile context for logging.
Radek Krejcib56c7502019-02-13 14:19:54 +01001197 * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature
1198 * being currently processed).
1199 * @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 +01001200 * @return LY_SUCCESS if everything is ok.
1201 * @return LY_EVALID if the feature references indirectly itself.
1202 */
1203static LY_ERR
1204lys_compile_feature_circular_check(struct lysc_ctx *ctx, struct lysc_feature *feature, struct lysc_feature **depfeatures)
1205{
1206 LY_ERR ret = LY_EVALID;
1207 unsigned int u, v;
1208 struct ly_set recursion = {0};
1209 struct lysc_feature *drv;
1210
1211 if (!depfeatures) {
1212 return LY_SUCCESS;
1213 }
1214
1215 for (u = 0; u < LY_ARRAY_SIZE(depfeatures); ++u) {
1216 if (feature == depfeatures[u]) {
1217 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1218 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
1219 goto cleanup;
1220 }
1221 ly_set_add(&recursion, depfeatures[u], 0);
1222 }
1223
1224 for (v = 0; v < recursion.count; ++v) {
1225 drv = recursion.objs[v];
1226 if (!drv->depfeatures) {
1227 continue;
1228 }
1229 for (u = 0; u < LY_ARRAY_SIZE(drv->depfeatures); ++u) {
1230 if (feature == drv->depfeatures[u]) {
1231 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1232 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
1233 goto cleanup;
1234 }
1235 ly_set_add(&recursion, drv->depfeatures[u], 0);
1236 }
1237 }
1238 ret = LY_SUCCESS;
1239
1240cleanup:
1241 ly_set_erase(&recursion, NULL);
1242 return ret;
1243}
1244
1245/**
Radek Krejci0af46292019-01-11 16:02:31 +01001246 * @brief Create pre-compiled features array.
1247 *
1248 * See lys_feature_precompile() for more details.
1249 *
Radek Krejcia3045382018-11-22 14:30:31 +01001250 * @param[in] ctx Compile context.
1251 * @param[in] feature_p Parsed feature definition to compile.
Radek Krejci0af46292019-01-11 16:02:31 +01001252 * @param[in,out] features List of already (pre)compiled features to find the corresponding precompiled feature structure.
Radek Krejcia3045382018-11-22 14:30:31 +01001253 * @return LY_ERR value.
1254 */
Radek Krejci19a96102018-11-15 13:38:09 +01001255static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02001256lys_feature_precompile_finish(struct lysc_ctx *ctx, struct lysp_feature *feature_p, struct lysc_feature *features)
Radek Krejci19a96102018-11-15 13:38:09 +01001257{
Radek Krejci0af46292019-01-11 16:02:31 +01001258 unsigned int u, v, x;
1259 struct lysc_feature *feature, **df;
Radek Krejci19a96102018-11-15 13:38:09 +01001260 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01001261
Radek Krejci327de162019-06-14 12:52:07 +02001262
Radek Krejci0af46292019-01-11 16:02:31 +01001263 /* find the preprecompiled feature */
1264 LY_ARRAY_FOR(features, x) {
1265 if (strcmp(features[x].name, feature_p->name)) {
1266 continue;
1267 }
1268 feature = &features[x];
Radek Krejci327de162019-06-14 12:52:07 +02001269 lysc_update_path(ctx, NULL, "{feature}");
1270 lysc_update_path(ctx, NULL, feature_p->name);
Radek Krejci19a96102018-11-15 13:38:09 +01001271
Radek Krejci0af46292019-01-11 16:02:31 +01001272 /* finish compilation started in lys_feature_precompile() */
Radek Krejci0935f412019-08-20 16:15:18 +02001273 COMPILE_EXTS_GOTO(ctx, feature_p->exts, feature->exts, feature, LYEXT_PAR_FEATURE, ret, done);
Radek Krejciec4da802019-05-02 13:02:41 +02001274 COMPILE_ARRAY_GOTO(ctx, feature_p->iffeatures, feature->iffeatures, u, lys_compile_iffeature, ret, done);
Radek Krejci0af46292019-01-11 16:02:31 +01001275 if (feature->iffeatures) {
1276 for (u = 0; u < LY_ARRAY_SIZE(feature->iffeatures); ++u) {
1277 if (feature->iffeatures[u].features) {
1278 for (v = 0; v < LY_ARRAY_SIZE(feature->iffeatures[u].features); ++v) {
Radek Krejci09a1fc52019-02-13 10:55:17 +01001279 /* check for circular dependency - direct reference first,... */
1280 if (feature == feature->iffeatures[u].features[v]) {
1281 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1282 "Feature \"%s\" is referenced from itself.", feature->name);
1283 return LY_EVALID;
1284 }
1285 /* ... and indirect circular reference */
1286 LY_CHECK_RET(lys_compile_feature_circular_check(ctx, feature->iffeatures[u].features[v], feature->depfeatures));
1287
Radek Krejci0af46292019-01-11 16:02:31 +01001288 /* add itself into the dependants list */
1289 LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM);
1290 *df = feature;
1291 }
Radek Krejci19a96102018-11-15 13:38:09 +01001292 }
Radek Krejci19a96102018-11-15 13:38:09 +01001293 }
1294 }
Radek Krejci327de162019-06-14 12:52:07 +02001295 lysc_update_path(ctx, NULL, NULL);
1296 lysc_update_path(ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001297 done:
1298 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01001299 }
Radek Krejci0af46292019-01-11 16:02:31 +01001300
1301 LOGINT(ctx->ctx);
1302 return LY_EINT;
Radek Krejci19a96102018-11-15 13:38:09 +01001303}
1304
Radek Krejcib56c7502019-02-13 14:19:54 +01001305/**
1306 * @brief Revert compiled list of features back to the precompiled state.
1307 *
1308 * Function is needed in case the compilation failed and the schema is expected to revert back to the non-compiled status.
1309 * The features are supposed to be stored again as off_features in ::lys_module structure.
1310 *
1311 * @param[in] ctx Compilation context.
1312 * @param[in] mod The module structure still holding the compiled (but possibly not finished, only the list of compiled features is taken) schema
1313 * and supposed to hold the off_features list.
1314 */
Radek Krejci95710c92019-02-11 15:49:55 +01001315static void
1316lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod)
1317{
1318 unsigned int u, v;
1319
1320 /* keep the off_features list until the complete lys_module is freed */
1321 mod->off_features = mod->compiled->features;
1322 mod->compiled->features = NULL;
1323
1324 /* in the off_features list, remove all the parts (from finished compiling process)
1325 * which may points into the data being freed here */
1326 LY_ARRAY_FOR(mod->off_features, u) {
1327 LY_ARRAY_FOR(mod->off_features[u].iffeatures, v) {
1328 lysc_iffeature_free(ctx->ctx, &mod->off_features[u].iffeatures[v]);
1329 }
1330 LY_ARRAY_FREE(mod->off_features[u].iffeatures);
1331 mod->off_features[u].iffeatures = NULL;
1332
1333 LY_ARRAY_FOR(mod->off_features[u].exts, v) {
1334 lysc_ext_instance_free(ctx->ctx, &(mod->off_features[u].exts)[v]);
1335 }
1336 LY_ARRAY_FREE(mod->off_features[u].exts);
1337 mod->off_features[u].exts = NULL;
1338 }
1339}
1340
Radek Krejcia3045382018-11-22 14:30:31 +01001341/**
1342 * @brief Validate and normalize numeric value from a range definition.
1343 * @param[in] ctx Compile context.
1344 * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to
1345 * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into
1346 * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from
1347 * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100.
1348 * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64.
1349 * @param[in] value String value of the range boundary.
1350 * @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.
1351 * @param[out] valcopy NULL-terminated string with the numeric value to parse and store.
1352 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value).
1353 */
Radek Krejcie88beef2019-05-30 15:47:19 +02001354LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001355range_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 +01001356{
Radek Krejci6cba4292018-11-15 17:33:29 +01001357 size_t fraction = 0, size;
1358
Radek Krejci19a96102018-11-15 13:38:09 +01001359 *len = 0;
1360
1361 assert(value);
1362 /* parse value */
1363 if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) {
1364 return LY_EVALID;
1365 }
1366
1367 if ((value[*len] == '-') || (value[*len] == '+')) {
1368 ++(*len);
1369 }
1370
1371 while (isdigit(value[*len])) {
1372 ++(*len);
1373 }
1374
1375 if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001376 if (basetype == LY_TYPE_DEC64) {
1377 goto decimal;
1378 } else {
1379 *valcopy = strndup(value, *len);
1380 return LY_SUCCESS;
1381 }
Radek Krejci19a96102018-11-15 13:38:09 +01001382 }
1383 fraction = *len;
1384
1385 ++(*len);
1386 while (isdigit(value[*len])) {
1387 ++(*len);
1388 }
1389
Radek Krejci6cba4292018-11-15 17:33:29 +01001390 if (basetype == LY_TYPE_DEC64) {
1391decimal:
1392 assert(frdigits);
Radek Krejci943177f2019-06-14 16:32:43 +02001393 if (fraction && (*len - 1 - fraction > frdigits)) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001394 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1395 "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.",
1396 *len, value, frdigits);
1397 return LY_EINVAL;
1398 }
1399 if (fraction) {
1400 size = (*len) + (frdigits - ((*len) - 1 - fraction));
1401 } else {
1402 size = (*len) + frdigits + 1;
1403 }
1404 *valcopy = malloc(size * sizeof **valcopy);
Radek Krejci19a96102018-11-15 13:38:09 +01001405 LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM);
1406
Radek Krejci6cba4292018-11-15 17:33:29 +01001407 (*valcopy)[size - 1] = '\0';
1408 if (fraction) {
1409 memcpy(&(*valcopy)[0], &value[0], fraction);
1410 memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction));
1411 memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction));
1412 } else {
1413 memcpy(&(*valcopy)[0], &value[0], *len);
1414 memset(&(*valcopy)[*len], '0', frdigits);
1415 }
Radek Krejci19a96102018-11-15 13:38:09 +01001416 }
1417 return LY_SUCCESS;
1418}
1419
Radek Krejcia3045382018-11-22 14:30:31 +01001420/**
1421 * @brief Check that values in range are in ascendant order.
1422 * @param[in] unsigned_value Flag to note that we are working with unsigned values.
Radek Krejci5969f272018-11-23 10:03:58 +01001423 * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous,
1424 * max can be also equal.
Radek Krejcia3045382018-11-22 14:30:31 +01001425 * @param[in] value Current value to check.
1426 * @param[in] prev_value The last seen value.
1427 * @return LY_SUCCESS or LY_EEXIST for invalid order.
1428 */
Radek Krejci19a96102018-11-15 13:38:09 +01001429static LY_ERR
Radek Krejci5969f272018-11-23 10:03:58 +01001430range_part_check_ascendancy(int unsigned_value, int max, int64_t value, int64_t prev_value)
Radek Krejci19a96102018-11-15 13:38:09 +01001431{
1432 if (unsigned_value) {
Radek Krejci5969f272018-11-23 10:03:58 +01001433 if ((max && (uint64_t)prev_value > (uint64_t)value) || (!max && (uint64_t)prev_value >= (uint64_t)value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001434 return LY_EEXIST;
1435 }
1436 } else {
Radek Krejci5969f272018-11-23 10:03:58 +01001437 if ((max && prev_value > value) || (!max && prev_value >= value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001438 return LY_EEXIST;
1439 }
1440 }
1441 return LY_SUCCESS;
1442}
1443
Radek Krejcia3045382018-11-22 14:30:31 +01001444/**
1445 * @brief Set min/max value of the range part.
1446 * @param[in] ctx Compile context.
1447 * @param[in] part Range part structure to fill.
1448 * @param[in] max Flag to distinguish if storing min or max value.
1449 * @param[in] prev The last seen value to check that all values in range are specified in ascendant order.
1450 * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules.
1451 * @param[in] first Flag for the first value of the range to avoid ascendancy order.
1452 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1453 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
Radek Krejci5969f272018-11-23 10:03:58 +01001454 * @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 +01001455 * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set.
1456 * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number),
1457 * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the
1458 * frdigits value), LY_EMEM.
1459 */
Radek Krejci19a96102018-11-15 13:38:09 +01001460static LY_ERR
1461range_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 +01001462 uint8_t frdigits, struct lysc_range *base_range, const char **value)
Radek Krejci19a96102018-11-15 13:38:09 +01001463{
1464 LY_ERR ret = LY_SUCCESS;
1465 char *valcopy = NULL;
1466 size_t len;
1467
1468 if (value) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001469 ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy);
Radek Krejci5969f272018-11-23 10:03:58 +01001470 LY_CHECK_GOTO(ret, finalize);
1471 }
1472 if (!valcopy && base_range) {
1473 if (max) {
1474 part->max_64 = base_range->parts[LY_ARRAY_SIZE(base_range->parts) - 1].max_64;
1475 } else {
1476 part->min_64 = base_range->parts[0].min_64;
1477 }
1478 if (!first) {
1479 ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev);
1480 }
1481 goto finalize;
Radek Krejci19a96102018-11-15 13:38:09 +01001482 }
1483
1484 switch (basetype) {
Radek Krejci19a96102018-11-15 13:38:09 +01001485 case LY_TYPE_INT8: /* range */
1486 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001487 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-128), INT64_C(127), 10, max ? &part->max_64 : &part->min_64);
Radek Krejci19a96102018-11-15 13:38:09 +01001488 } else if (max) {
1489 part->max_64 = INT64_C(127);
1490 } else {
1491 part->min_64 = INT64_C(-128);
1492 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001493 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001494 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001495 }
1496 break;
1497 case LY_TYPE_INT16: /* range */
1498 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001499 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-32768), INT64_C(32767), 10, max ? &part->max_64 : &part->min_64);
Radek Krejci19a96102018-11-15 13:38:09 +01001500 } else if (max) {
1501 part->max_64 = INT64_C(32767);
1502 } else {
1503 part->min_64 = INT64_C(-32768);
1504 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001505 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001506 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001507 }
1508 break;
1509 case LY_TYPE_INT32: /* range */
1510 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001511 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-2147483648), INT64_C(2147483647), 10, max ? &part->max_64 : &part->min_64);
Radek Krejci19a96102018-11-15 13:38:09 +01001512 } else if (max) {
1513 part->max_64 = INT64_C(2147483647);
1514 } else {
1515 part->min_64 = INT64_C(-2147483648);
1516 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001517 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001518 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001519 }
1520 break;
1521 case LY_TYPE_INT64: /* range */
Radek Krejci25cfef72018-11-23 14:15:52 +01001522 case LY_TYPE_DEC64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001523 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001524 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807), 10,
Radek Krejci19a96102018-11-15 13:38:09 +01001525 max ? &part->max_64 : &part->min_64);
1526 } else if (max) {
1527 part->max_64 = INT64_C(9223372036854775807);
1528 } else {
1529 part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1);
1530 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001531 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001532 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001533 }
1534 break;
1535 case LY_TYPE_UINT8: /* range */
1536 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001537 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(255), 10, max ? &part->max_u64 : &part->min_u64);
Radek Krejci19a96102018-11-15 13:38:09 +01001538 } else if (max) {
1539 part->max_u64 = UINT64_C(255);
1540 } else {
1541 part->min_u64 = UINT64_C(0);
1542 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001543 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001544 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001545 }
1546 break;
1547 case LY_TYPE_UINT16: /* range */
1548 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001549 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(65535), 10, max ? &part->max_u64 : &part->min_u64);
Radek Krejci19a96102018-11-15 13:38:09 +01001550 } else if (max) {
1551 part->max_u64 = UINT64_C(65535);
1552 } else {
1553 part->min_u64 = UINT64_C(0);
1554 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001555 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001556 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001557 }
1558 break;
1559 case LY_TYPE_UINT32: /* range */
1560 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001561 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(4294967295), 10, max ? &part->max_u64 : &part->min_u64);
Radek Krejci19a96102018-11-15 13:38:09 +01001562 } else if (max) {
1563 part->max_u64 = UINT64_C(4294967295);
1564 } else {
1565 part->min_u64 = UINT64_C(0);
1566 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001567 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001568 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001569 }
1570 break;
1571 case LY_TYPE_UINT64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001572 case LY_TYPE_STRING: /* length */
Radek Krejci25cfef72018-11-23 14:15:52 +01001573 case LY_TYPE_BINARY: /* length */
Radek Krejci19a96102018-11-15 13:38:09 +01001574 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001575 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(18446744073709551615), 10, max ? &part->max_u64 : &part->min_u64);
Radek Krejci19a96102018-11-15 13:38:09 +01001576 } else if (max) {
1577 part->max_u64 = UINT64_C(18446744073709551615);
1578 } else {
1579 part->min_u64 = UINT64_C(0);
1580 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001581 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001582 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001583 }
1584 break;
1585 default:
1586 LOGINT(ctx->ctx);
1587 ret = LY_EINT;
1588 }
1589
Radek Krejci5969f272018-11-23 10:03:58 +01001590finalize:
Radek Krejci19a96102018-11-15 13:38:09 +01001591 if (ret == LY_EDENIED) {
1592 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1593 "Invalid %s restriction - value \"%s\" does not fit the type limitations.",
1594 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1595 } else if (ret == LY_EVALID) {
1596 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1597 "Invalid %s restriction - invalid value \"%s\".",
1598 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1599 } else if (ret == LY_EEXIST) {
1600 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1601 "Invalid %s restriction - values are not in ascending order (%s).",
Radek Krejci6cba4292018-11-15 17:33:29 +01001602 length_restr ? "length" : "range",
Radek Krejci5969f272018-11-23 10:03:58 +01001603 (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min");
Radek Krejci19a96102018-11-15 13:38:09 +01001604 } else if (!ret && value) {
1605 *value = *value + len;
1606 }
1607 free(valcopy);
1608 return ret;
1609}
1610
Radek Krejcia3045382018-11-22 14:30:31 +01001611/**
1612 * @brief Compile the parsed range restriction.
1613 * @param[in] ctx Compile context.
1614 * @param[in] range_p Parsed range structure to compile.
1615 * @param[in] basetype Base YANG built-in type of the node with the range restriction.
1616 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1617 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
1618 * @param[in] base_range Range restriction of the type from which the current type is derived. The current
1619 * range restriction must be more restrictive than the base_range.
1620 * @param[in,out] range Pointer to the created current range structure.
1621 * @return LY_ERR value.
1622 */
Radek Krejci19a96102018-11-15 13:38:09 +01001623static LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001624lys_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 +01001625 struct lysc_range *base_range, struct lysc_range **range)
1626{
1627 LY_ERR ret = LY_EVALID;
1628 const char *expr;
1629 struct lysc_range_part *parts = NULL, *part;
1630 int range_expected = 0, uns;
1631 unsigned int parts_done = 0, u, v;
1632
1633 assert(range);
1634 assert(range_p);
1635
1636 expr = range_p->arg;
1637 while(1) {
1638 if (isspace(*expr)) {
1639 ++expr;
1640 } else if (*expr == '\0') {
1641 if (range_expected) {
1642 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1643 "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).",
1644 length_restr ? "length" : "range", range_p->arg);
1645 goto cleanup;
1646 } else if (!parts || parts_done == LY_ARRAY_SIZE(parts)) {
1647 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1648 "Invalid %s restriction - unexpected end of the expression (%s).",
1649 length_restr ? "length" : "range", range_p->arg);
1650 goto cleanup;
1651 }
1652 parts_done++;
1653 break;
1654 } else if (!strncmp(expr, "min", 3)) {
1655 if (parts) {
1656 /* min cannot be used elsewhere than in the first part */
1657 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1658 "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range",
1659 expr - range_p->arg, range_p->arg);
1660 goto cleanup;
1661 }
1662 expr += 3;
1663
1664 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Radek Krejci5969f272018-11-23 10:03:58 +01001665 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 +01001666 part->max_64 = part->min_64;
1667 } else if (*expr == '|') {
1668 if (!parts || range_expected) {
1669 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1670 "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr);
1671 goto cleanup;
1672 }
1673 expr++;
1674 parts_done++;
1675 /* process next part of the expression */
1676 } else if (!strncmp(expr, "..", 2)) {
1677 expr += 2;
1678 while (isspace(*expr)) {
1679 expr++;
1680 }
1681 if (!parts || LY_ARRAY_SIZE(parts) == parts_done) {
1682 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1683 "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range");
1684 goto cleanup;
1685 }
1686 /* continue expecting the upper boundary */
1687 range_expected = 1;
1688 } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) {
1689 /* number */
1690 if (range_expected) {
1691 part = &parts[LY_ARRAY_SIZE(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001692 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 +01001693 range_expected = 0;
1694 } else {
1695 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
1696 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 +01001697 basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001698 part->max_64 = part->min_64;
1699 }
1700
1701 /* continue with possible another expression part */
1702 } else if (!strncmp(expr, "max", 3)) {
1703 expr += 3;
1704 while (isspace(*expr)) {
1705 expr++;
1706 }
1707 if (*expr != '\0') {
1708 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).",
1709 length_restr ? "length" : "range", expr);
1710 goto cleanup;
1711 }
1712 if (range_expected) {
1713 part = &parts[LY_ARRAY_SIZE(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001714 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 +01001715 range_expected = 0;
1716 } else {
1717 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
1718 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 +01001719 basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001720 part->min_64 = part->max_64;
1721 }
1722 } else {
1723 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).",
1724 length_restr ? "length" : "range", expr);
1725 goto cleanup;
1726 }
1727 }
1728
1729 /* check with the previous range/length restriction */
1730 if (base_range) {
1731 switch (basetype) {
1732 case LY_TYPE_BINARY:
1733 case LY_TYPE_UINT8:
1734 case LY_TYPE_UINT16:
1735 case LY_TYPE_UINT32:
1736 case LY_TYPE_UINT64:
1737 case LY_TYPE_STRING:
1738 uns = 1;
1739 break;
1740 case LY_TYPE_DEC64:
1741 case LY_TYPE_INT8:
1742 case LY_TYPE_INT16:
1743 case LY_TYPE_INT32:
1744 case LY_TYPE_INT64:
1745 uns = 0;
1746 break;
1747 default:
1748 LOGINT(ctx->ctx);
1749 ret = LY_EINT;
1750 goto cleanup;
1751 }
1752 for (u = v = 0; u < parts_done && v < LY_ARRAY_SIZE(base_range->parts); ++u) {
1753 if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) {
1754 goto baseerror;
1755 }
1756 /* current lower bound is not lower than the base */
1757 if (base_range->parts[v].min_64 == base_range->parts[v].max_64) {
1758 /* base has single value */
1759 if (base_range->parts[v].min_64 == parts[u].min_64) {
1760 /* both lower bounds are the same */
1761 if (parts[u].min_64 != parts[u].max_64) {
1762 /* current continues with a range */
1763 goto baseerror;
1764 } else {
1765 /* equal single values, move both forward */
1766 ++v;
1767 continue;
1768 }
1769 } else {
1770 /* base is single value lower than current range, so the
1771 * value from base range is removed in the current,
1772 * move only base and repeat checking */
1773 ++v;
1774 --u;
1775 continue;
1776 }
1777 } else {
1778 /* base is the range */
1779 if (parts[u].min_64 == parts[u].max_64) {
1780 /* current is a single value */
1781 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1782 /* current is behind the base range, so base range is omitted,
1783 * move the base and keep the current for further check */
1784 ++v;
1785 --u;
1786 } /* else it is within the base range, so move the current, but keep the base */
1787 continue;
1788 } else {
1789 /* both are ranges - check the higher bound, the lower was already checked */
1790 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1791 /* higher bound is higher than the current higher bound */
1792 if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) {
1793 /* but the current lower bound is also higher, so the base range is omitted,
1794 * continue with the same current, but move the base */
1795 --u;
1796 ++v;
1797 continue;
1798 }
1799 /* current range starts within the base range but end behind it */
1800 goto baseerror;
1801 } else {
1802 /* current range is smaller than the base,
1803 * move current, but stay with the base */
1804 continue;
1805 }
1806 }
1807 }
1808 }
1809 if (u != parts_done) {
1810baseerror:
1811 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1812 "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.",
1813 length_restr ? "length" : "range", range_p->arg);
1814 goto cleanup;
1815 }
1816 }
1817
1818 if (!(*range)) {
1819 *range = calloc(1, sizeof **range);
1820 LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM);
1821 }
1822
Radek Krejcic8b31002019-01-08 10:24:45 +01001823 /* we rewrite the following values as the types chain is being processed */
Radek Krejci19a96102018-11-15 13:38:09 +01001824 if (range_p->eapptag) {
1825 lydict_remove(ctx->ctx, (*range)->eapptag);
1826 (*range)->eapptag = lydict_insert(ctx->ctx, range_p->eapptag, 0);
1827 }
1828 if (range_p->emsg) {
1829 lydict_remove(ctx->ctx, (*range)->emsg);
1830 (*range)->emsg = lydict_insert(ctx->ctx, range_p->emsg, 0);
1831 }
Radek Krejcic8b31002019-01-08 10:24:45 +01001832 if (range_p->dsc) {
1833 lydict_remove(ctx->ctx, (*range)->dsc);
1834 (*range)->dsc = lydict_insert(ctx->ctx, range_p->dsc, 0);
1835 }
1836 if (range_p->ref) {
1837 lydict_remove(ctx->ctx, (*range)->ref);
1838 (*range)->ref = lydict_insert(ctx->ctx, range_p->ref, 0);
1839 }
Radek Krejci19a96102018-11-15 13:38:09 +01001840 /* extensions are taken only from the last range by the caller */
1841
1842 (*range)->parts = parts;
1843 parts = NULL;
1844 ret = LY_SUCCESS;
1845cleanup:
Radek Krejci19a96102018-11-15 13:38:09 +01001846 LY_ARRAY_FREE(parts);
1847
1848 return ret;
1849}
1850
1851/**
1852 * @brief Checks pattern syntax.
1853 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02001854 * @param[in] ctx Context.
1855 * @param[in] log_path Path for logging errors.
Radek Krejci19a96102018-11-15 13:38:09 +01001856 * @param[in] pattern Pattern to check.
Radek Krejci54579462019-04-30 12:47:06 +02001857 * @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 +01001858 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID.
Radek Krejci19a96102018-11-15 13:38:09 +01001859 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001860LY_ERR
1861lys_compile_type_pattern_check(struct ly_ctx *ctx, const char *log_path, const char *pattern, pcre2_code **code)
Radek Krejci19a96102018-11-15 13:38:09 +01001862{
Radek Krejci54579462019-04-30 12:47:06 +02001863 int idx, idx2, start, end, count;
Radek Krejci19a96102018-11-15 13:38:09 +01001864 char *perl_regex, *ptr;
Radek Krejci54579462019-04-30 12:47:06 +02001865 int err_code;
1866 const char *orig_ptr;
1867 PCRE2_SIZE err_offset;
1868 pcre2_code *code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01001869#define URANGE_LEN 19
1870 char *ublock2urange[][2] = {
1871 {"BasicLatin", "[\\x{0000}-\\x{007F}]"},
1872 {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"},
1873 {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"},
1874 {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"},
1875 {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"},
1876 {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"},
1877 {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"},
1878 {"Greek", "[\\x{0370}-\\x{03FF}]"},
1879 {"Cyrillic", "[\\x{0400}-\\x{04FF}]"},
1880 {"Armenian", "[\\x{0530}-\\x{058F}]"},
1881 {"Hebrew", "[\\x{0590}-\\x{05FF}]"},
1882 {"Arabic", "[\\x{0600}-\\x{06FF}]"},
1883 {"Syriac", "[\\x{0700}-\\x{074F}]"},
1884 {"Thaana", "[\\x{0780}-\\x{07BF}]"},
1885 {"Devanagari", "[\\x{0900}-\\x{097F}]"},
1886 {"Bengali", "[\\x{0980}-\\x{09FF}]"},
1887 {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"},
1888 {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"},
1889 {"Oriya", "[\\x{0B00}-\\x{0B7F}]"},
1890 {"Tamil", "[\\x{0B80}-\\x{0BFF}]"},
1891 {"Telugu", "[\\x{0C00}-\\x{0C7F}]"},
1892 {"Kannada", "[\\x{0C80}-\\x{0CFF}]"},
1893 {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"},
1894 {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"},
1895 {"Thai", "[\\x{0E00}-\\x{0E7F}]"},
1896 {"Lao", "[\\x{0E80}-\\x{0EFF}]"},
1897 {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"},
1898 {"Myanmar", "[\\x{1000}-\\x{109F}]"},
1899 {"Georgian", "[\\x{10A0}-\\x{10FF}]"},
1900 {"HangulJamo", "[\\x{1100}-\\x{11FF}]"},
1901 {"Ethiopic", "[\\x{1200}-\\x{137F}]"},
1902 {"Cherokee", "[\\x{13A0}-\\x{13FF}]"},
1903 {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"},
1904 {"Ogham", "[\\x{1680}-\\x{169F}]"},
1905 {"Runic", "[\\x{16A0}-\\x{16FF}]"},
1906 {"Khmer", "[\\x{1780}-\\x{17FF}]"},
1907 {"Mongolian", "[\\x{1800}-\\x{18AF}]"},
1908 {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"},
1909 {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"},
1910 {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"},
1911 {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"},
1912 {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"},
1913 {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"},
1914 {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"},
1915 {"NumberForms", "[\\x{2150}-\\x{218F}]"},
1916 {"Arrows", "[\\x{2190}-\\x{21FF}]"},
1917 {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"},
1918 {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"},
1919 {"ControlPictures", "[\\x{2400}-\\x{243F}]"},
1920 {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"},
1921 {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"},
1922 {"BoxDrawing", "[\\x{2500}-\\x{257F}]"},
1923 {"BlockElements", "[\\x{2580}-\\x{259F}]"},
1924 {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"},
1925 {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"},
1926 {"Dingbats", "[\\x{2700}-\\x{27BF}]"},
1927 {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"},
1928 {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"},
1929 {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"},
1930 {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"},
1931 {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"},
1932 {"Hiragana", "[\\x{3040}-\\x{309F}]"},
1933 {"Katakana", "[\\x{30A0}-\\x{30FF}]"},
1934 {"Bopomofo", "[\\x{3100}-\\x{312F}]"},
1935 {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"},
1936 {"Kanbun", "[\\x{3190}-\\x{319F}]"},
1937 {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"},
1938 {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"},
1939 {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"},
1940 {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"},
1941 {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"},
1942 {"YiSyllables", "[\\x{A000}-\\x{A48F}]"},
1943 {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"},
1944 {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"},
1945 {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"},
1946 {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"},
1947 {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"},
1948 {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"},
1949 {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"},
1950 {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"},
1951 {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"},
1952 {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"},
1953 {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"},
1954 {NULL, NULL}
1955 };
1956
1957 /* adjust the expression to a Perl equivalent
1958 * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */
1959
Michal Vasko03ff5a72019-09-11 13:49:33 +02001960 /* we need to replace all "$" and "^" with "\$" and "\^", count them now */
Radek Krejci19a96102018-11-15 13:38:09 +01001961 for (count = 0, ptr = strpbrk(pattern, "^$"); ptr; ++count, ptr = strpbrk(ptr + 1, "^$"));
1962
1963 perl_regex = malloc((strlen(pattern) + 4 + count) * sizeof(char));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001964 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
Radek Krejci19a96102018-11-15 13:38:09 +01001965 perl_regex[0] = '\0';
1966
1967 ptr = perl_regex;
1968
Radek Krejci19a96102018-11-15 13:38:09 +01001969 for (orig_ptr = pattern; orig_ptr[0]; ++orig_ptr) {
1970 if (orig_ptr[0] == '$') {
1971 ptr += sprintf(ptr, "\\$");
1972 } else if (orig_ptr[0] == '^') {
1973 ptr += sprintf(ptr, "\\^");
1974 } else {
1975 ptr[0] = orig_ptr[0];
1976 ++ptr;
1977 }
1978 }
Radek Krejci5819f7c2019-05-31 14:53:29 +02001979 ptr[0] = '\0';
1980 ++ptr;
Radek Krejci19a96102018-11-15 13:38:09 +01001981
1982 /* substitute Unicode Character Blocks with exact Character Ranges */
1983 while ((ptr = strstr(perl_regex, "\\p{Is"))) {
1984 start = ptr - perl_regex;
1985
1986 ptr = strchr(ptr, '}');
1987 if (!ptr) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001988 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP,
Radek Krejci19a96102018-11-15 13:38:09 +01001989 pattern, perl_regex + start + 2, "unterminated character property");
1990 free(perl_regex);
1991 return LY_EVALID;
1992 }
1993 end = (ptr - perl_regex) + 1;
1994
1995 /* need more space */
1996 if (end - start < URANGE_LEN) {
1997 perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001998 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx); free(perl_regex), LY_EMEM);
Radek Krejci19a96102018-11-15 13:38:09 +01001999 }
2000
2001 /* find our range */
2002 for (idx = 0; ublock2urange[idx][0]; ++idx) {
2003 if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) {
2004 break;
2005 }
2006 }
2007 if (!ublock2urange[idx][0]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002008 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP,
Radek Krejci19a96102018-11-15 13:38:09 +01002009 pattern, perl_regex + start + 5, "unknown block name");
2010 free(perl_regex);
2011 return LY_EVALID;
2012 }
2013
2014 /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */
2015 for (idx2 = 0, count = 0; idx2 < start; ++idx2) {
2016 if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
2017 ++count;
2018 }
2019 if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
2020 --count;
2021 }
2022 }
2023 if (count) {
2024 /* skip brackets */
2025 memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1);
2026 memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2);
2027 } else {
2028 memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1);
2029 memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN);
2030 }
2031 }
2032
2033 /* must return 0, already checked during parsing */
Radek Krejci5819f7c2019-05-31 14:53:29 +02002034 code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED,
2035 PCRE2_UTF | PCRE2_ANCHORED | PCRE2_ENDANCHORED | PCRE2_DOLLAR_ENDONLY | PCRE2_NO_AUTO_CAPTURE,
Radek Krejci54579462019-04-30 12:47:06 +02002036 &err_code, &err_offset, NULL);
2037 if (!code_local) {
2038 PCRE2_UCHAR err_msg[256] = {0};
2039 pcre2_get_error_message(err_code, err_msg, 256);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002040 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
Radek Krejci19a96102018-11-15 13:38:09 +01002041 free(perl_regex);
2042 return LY_EVALID;
2043 }
2044 free(perl_regex);
2045
Radek Krejci54579462019-04-30 12:47:06 +02002046 if (code) {
2047 *code = code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01002048 } else {
Radek Krejci54579462019-04-30 12:47:06 +02002049 free(code_local);
Radek Krejci19a96102018-11-15 13:38:09 +01002050 }
2051
2052 return LY_SUCCESS;
2053
2054#undef URANGE_LEN
2055}
2056
Radek Krejcia3045382018-11-22 14:30:31 +01002057/**
2058 * @brief Compile parsed pattern restriction in conjunction with the patterns from base type.
2059 * @param[in] ctx Compile context.
2060 * @param[in] patterns_p Array of parsed patterns from the current type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002061 * @param[in] base_patterns Compiled patterns from the type from which the current type is derived.
2062 * Patterns from the base type are inherited to have all the patterns that have to match at one place.
2063 * @param[out] patterns Pointer to the storage for the patterns of the current type.
2064 * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID.
2065 */
Radek Krejci19a96102018-11-15 13:38:09 +01002066static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02002067lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p,
Radek Krejci19a96102018-11-15 13:38:09 +01002068 struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns)
2069{
2070 struct lysc_pattern **pattern;
Radek Krejci0935f412019-08-20 16:15:18 +02002071 unsigned int u;
Radek Krejci19a96102018-11-15 13:38:09 +01002072 LY_ERR ret = LY_SUCCESS;
2073
2074 /* first, copy the patterns from the base type */
2075 if (base_patterns) {
2076 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
2077 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
2078 }
2079
2080 LY_ARRAY_FOR(patterns_p, u) {
2081 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
2082 *pattern = calloc(1, sizeof **pattern);
2083 ++(*pattern)->refcount;
2084
Michal Vasko03ff5a72019-09-11 13:49:33 +02002085 ret = lys_compile_type_pattern_check(ctx->ctx, ctx->path, &patterns_p[u].arg[1], &(*pattern)->code);
Radek Krejci19a96102018-11-15 13:38:09 +01002086 LY_CHECK_RET(ret);
Radek Krejci19a96102018-11-15 13:38:09 +01002087
2088 if (patterns_p[u].arg[0] == 0x15) {
2089 (*pattern)->inverted = 1;
2090 }
Radek Krejci54579462019-04-30 12:47:06 +02002091 DUP_STRING(ctx->ctx, &patterns_p[u].arg[1], (*pattern)->expr);
Radek Krejci19a96102018-11-15 13:38:09 +01002092 DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag);
2093 DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +01002094 DUP_STRING(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc);
2095 DUP_STRING(ctx->ctx, patterns_p[u].ref, (*pattern)->ref);
Radek Krejci0935f412019-08-20 16:15:18 +02002096 COMPILE_EXTS_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts, (*pattern), LYEXT_PAR_PATTERN, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01002097 }
2098done:
2099 return ret;
2100}
2101
Radek Krejcia3045382018-11-22 14:30:31 +01002102/**
2103 * @brief map of the possible restrictions combination for the specific built-in type.
2104 */
Radek Krejci19a96102018-11-15 13:38:09 +01002105static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
2106 0 /* LY_TYPE_UNKNOWN */,
2107 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
Radek Krejci5969f272018-11-23 10:03:58 +01002108 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
2109 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
2110 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
2111 LYS_SET_RANGE /* LY_TYPE_UINT64 */,
2112 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
Radek Krejci19a96102018-11-15 13:38:09 +01002113 LYS_SET_BIT /* LY_TYPE_BITS */,
2114 0 /* LY_TYPE_BOOL */,
2115 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
2116 0 /* LY_TYPE_EMPTY */,
2117 LYS_SET_ENUM /* LY_TYPE_ENUM */,
2118 LYS_SET_BASE /* LY_TYPE_IDENT */,
2119 LYS_SET_REQINST /* LY_TYPE_INST */,
2120 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
Radek Krejci19a96102018-11-15 13:38:09 +01002121 LYS_SET_TYPE /* LY_TYPE_UNION */,
2122 LYS_SET_RANGE /* LY_TYPE_INT8 */,
Radek Krejci19a96102018-11-15 13:38:09 +01002123 LYS_SET_RANGE /* LY_TYPE_INT16 */,
Radek Krejci19a96102018-11-15 13:38:09 +01002124 LYS_SET_RANGE /* LY_TYPE_INT32 */,
Radek Krejci5969f272018-11-23 10:03:58 +01002125 LYS_SET_RANGE /* LY_TYPE_INT64 */
2126};
2127
2128/**
2129 * @brief stringification of the YANG built-in data types
2130 */
2131const char* ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer",
2132 "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration",
2133 "identityref", "instance-identifier", "leafref", "union", "8bit integer", "16bit integer", "32bit integer", "64bit integer"
Radek Krejci19a96102018-11-15 13:38:09 +01002134};
2135
Radek Krejcia3045382018-11-22 14:30:31 +01002136/**
2137 * @brief Compile parsed type's enum structures (for enumeration and bits types).
2138 * @param[in] ctx Compile context.
2139 * @param[in] enums_p Array of the parsed enum structures to compile.
2140 * @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 +01002141 * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible.
2142 * @param[out] enums Newly created array of the compiled enums information for the current type.
2143 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2144 */
Radek Krejci19a96102018-11-15 13:38:09 +01002145static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02002146lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype,
Radek Krejci693262f2019-04-29 15:23:20 +02002147 struct lysc_type_bitenum_item *base_enums, struct lysc_type_bitenum_item **enums)
Radek Krejci19a96102018-11-15 13:38:09 +01002148{
2149 LY_ERR ret = LY_SUCCESS;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02002150 unsigned int u, v, match = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002151 int32_t value = 0;
2152 uint32_t position = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02002153 struct lysc_type_bitenum_item *e, storage;
Radek Krejci19a96102018-11-15 13:38:09 +01002154
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002155 if (base_enums && ctx->mod_def->version < 2) {
Radek Krejci19a96102018-11-15 13:38:09 +01002156 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
2157 basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
2158 return LY_EVALID;
2159 }
2160
2161 LY_ARRAY_FOR(enums_p, u) {
2162 LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM);
2163 DUP_STRING(ctx->ctx, enums_p[u].name, e->name);
Radek Krejcic8b31002019-01-08 10:24:45 +01002164 DUP_STRING(ctx->ctx, enums_p[u].ref, e->dsc);
2165 DUP_STRING(ctx->ctx, enums_p[u].ref, e->ref);
Radek Krejci693262f2019-04-29 15:23:20 +02002166 e->flags = enums_p[u].flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01002167 if (base_enums) {
2168 /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
2169 LY_ARRAY_FOR(base_enums, v) {
2170 if (!strcmp(e->name, base_enums[v].name)) {
2171 break;
2172 }
2173 }
2174 if (v == LY_ARRAY_SIZE(base_enums)) {
2175 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2176 "Invalid %s - derived type adds new item \"%s\".",
2177 basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name);
2178 return LY_EVALID;
2179 }
2180 match = v;
2181 }
2182
2183 if (basetype == LY_TYPE_ENUM) {
Radek Krejci693262f2019-04-29 15:23:20 +02002184 e->flags |= LYS_ISENUM;
Radek Krejci19a96102018-11-15 13:38:09 +01002185 if (enums_p[u].flags & LYS_SET_VALUE) {
2186 e->value = (int32_t)enums_p[u].value;
2187 if (!u || e->value >= value) {
2188 value = e->value + 1;
2189 }
2190 /* check collision with other values */
2191 for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) {
2192 if (e->value == (*enums)[v].value) {
2193 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2194 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
2195 e->value, e->name, (*enums)[v].name);
2196 return LY_EVALID;
2197 }
2198 }
2199 } else if (base_enums) {
2200 /* inherit the assigned value */
2201 e->value = base_enums[match].value;
2202 if (!u || e->value >= value) {
2203 value = e->value + 1;
2204 }
2205 } else {
2206 /* assign value automatically */
2207 if (u && value == INT32_MIN) {
2208 /* counter overflow */
2209 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2210 "Invalid enumeration - it is not possible to auto-assign enum value for "
2211 "\"%s\" since the highest value is already 2147483647.", e->name);
2212 return LY_EVALID;
2213 }
2214 e->value = value++;
2215 }
2216 } else { /* LY_TYPE_BITS */
2217 if (enums_p[u].flags & LYS_SET_VALUE) {
2218 e->value = (int32_t)enums_p[u].value;
2219 if (!u || (uint32_t)e->value >= position) {
2220 position = (uint32_t)e->value + 1;
2221 }
2222 /* check collision with other values */
2223 for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) {
2224 if (e->value == (*enums)[v].value) {
2225 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2226 "Invalid bits - position %u collide in items \"%s\" and \"%s\".",
2227 (uint32_t)e->value, e->name, (*enums)[v].name);
2228 return LY_EVALID;
2229 }
2230 }
2231 } else if (base_enums) {
2232 /* inherit the assigned value */
2233 e->value = base_enums[match].value;
2234 if (!u || (uint32_t)e->value >= position) {
2235 position = (uint32_t)e->value + 1;
2236 }
2237 } else {
2238 /* assign value automatically */
2239 if (u && position == 0) {
2240 /* counter overflow */
2241 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2242 "Invalid bits - it is not possible to auto-assign bit position for "
2243 "\"%s\" since the highest value is already 4294967295.", e->name);
2244 return LY_EVALID;
2245 }
2246 e->value = position++;
2247 }
2248 }
2249
2250 if (base_enums) {
2251 /* the assigned values must not change from the derived type */
2252 if (e->value != base_enums[match].value) {
2253 if (basetype == LY_TYPE_ENUM) {
2254 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2255 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
2256 e->name, base_enums[match].value, e->value);
2257 } else {
2258 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2259 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
2260 e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value);
2261 }
2262 return LY_EVALID;
2263 }
2264 }
2265
Radek Krejciec4da802019-05-02 13:02:41 +02002266 COMPILE_ARRAY_GOTO(ctx, enums_p[u].iffeatures, e->iffeatures, v, lys_compile_iffeature, ret, done);
Radek Krejci0935f412019-08-20 16:15:18 +02002267 COMPILE_EXTS_GOTO(ctx, enums_p[u].exts, e->exts, e, basetype == LY_TYPE_ENUM ? LYEXT_PAR_TYPE_ENUM : LYEXT_PAR_TYPE_BIT, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01002268
2269 if (basetype == LY_TYPE_BITS) {
2270 /* keep bits ordered by position */
2271 for (v = u; v && (*enums)[v - 1].value > e->value; --v);
2272 if (v != u) {
2273 memcpy(&storage, e, sizeof *e);
2274 memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums);
2275 memcpy(&(*enums)[v], &storage, sizeof storage);
2276 }
2277 }
2278 }
2279
2280done:
2281 return ret;
2282}
2283
Radek Krejcia3045382018-11-22 14:30:31 +01002284#define MOVE_PATH_PARENT(NODE, LIMIT_COND, TERM, ERR_MSG, ...) \
2285 for ((NODE) = (NODE)->parent; \
Michal Vasko03ff5a72019-09-11 13:49:33 +02002286 (NODE) && !((NODE)->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_NOTIF | LYS_ACTION)); \
Radek Krejcia3045382018-11-22 14:30:31 +01002287 (NODE) = (NODE)->parent); \
2288 if (!(NODE) && (LIMIT_COND)) { /* we are going higher than top-level */ \
2289 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, ERR_MSG, ##__VA_ARGS__); \
2290 TERM; \
2291 }
2292
2293/**
2294 * @brief Validate the predicate(s) from the leafref path.
2295 * @param[in] ctx Compile context
2296 * @param[in, out] predicate Pointer to the predicate in the leafref path. The pointer is moved after the validated predicate(s).
2297 * Since there can be multiple adjacent predicates for lists with multiple keys, all such predicates are validated.
Radek Krejci4ce68932018-11-28 12:53:36 +01002298 * @param[in] start_node Path context node (where the path is instantiated).
Radek Krejcia3045382018-11-22 14:30:31 +01002299 * @param[in] context_node Predicate context node (where the predicate is placed).
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002300 * @param[in] path_context Schema where the path was defined to correct resolve of the prefixes.
Radek Krejcia3045382018-11-22 14:30:31 +01002301 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2302 */
2303static LY_ERR
Radek Krejci4ce68932018-11-28 12:53:36 +01002304lys_compile_leafref_predicate_validate(struct lysc_ctx *ctx, const char **predicate, const struct lysc_node *start_node,
Radek Krejcibade82a2018-12-05 10:13:48 +01002305 const struct lysc_node_list *context_node, const struct lys_module *path_context)
Radek Krejcia3045382018-11-22 14:30:31 +01002306{
2307 LY_ERR ret = LY_EVALID;
2308 const struct lys_module *mod;
2309 const struct lysc_node *src_node, *dst_node;
2310 const char *path_key_expr, *pke_start, *src, *src_prefix, *dst, *dst_prefix;
2311 size_t src_len, src_prefix_len, dst_len, dst_prefix_len;
Radek Krejci0fe9b512019-07-26 17:51:05 +02002312 unsigned int dest_parent_times, c;
Radek Krejcia3045382018-11-22 14:30:31 +01002313 const char *start, *end, *pke_end;
2314 struct ly_set keys = {0};
2315 int i;
2316
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002317 assert(path_context);
2318
Radek Krejcia3045382018-11-22 14:30:31 +01002319 while (**predicate == '[') {
2320 start = (*predicate)++;
2321
2322 while (isspace(**predicate)) {
2323 ++(*predicate);
2324 }
Radek Krejcib4a4a272019-06-10 12:44:52 +02002325 LY_CHECK_GOTO(ly_parse_nodeid(predicate, &src_prefix, &src_prefix_len, &src, &src_len), cleanup);
Radek Krejcia3045382018-11-22 14:30:31 +01002326 while (isspace(**predicate)) {
2327 ++(*predicate);
2328 }
2329 if (**predicate != '=') {
2330 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01002331 "Invalid leafref path predicate \"%.*s\" - missing \"=\" after node-identifier.",
2332 *predicate - start + 1, start);
Radek Krejcia3045382018-11-22 14:30:31 +01002333 goto cleanup;
2334 }
2335 ++(*predicate);
2336 while (isspace(**predicate)) {
2337 ++(*predicate);
2338 }
2339
2340 if ((end = pke_end = strchr(*predicate, ']')) == NULL) {
2341 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2342 "Invalid leafref path predicate \"%s\" - missing predicate termination.", start);
2343 goto cleanup;
2344 }
2345 --pke_end;
2346 while (isspace(*pke_end)) {
2347 --pke_end;
2348 }
Radek Krejci7adf4ff2018-12-05 08:55:00 +01002349 ++pke_end;
Radek Krejcia3045382018-11-22 14:30:31 +01002350 /* localize path-key-expr */
2351 pke_start = path_key_expr = *predicate;
2352 /* move after the current predicate */
2353 *predicate = end + 1;
2354
2355 /* source (must be leaf or leaf-list) */
2356 if (src_prefix) {
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002357 mod = lys_module_find_prefix(path_context, src_prefix, src_prefix_len);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002358 if (!mod) {
2359 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2360 "Invalid leafref path predicate \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".",
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002361 *predicate - start, start, src_prefix_len, src_prefix, path_context->name);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002362 goto cleanup;
2363 }
Radek Krejci92cc8512019-04-25 09:57:06 +02002364 if (!mod->implemented) {
2365 /* make the module implemented */
Radek Krejci77a8bcd2019-09-11 11:20:02 +02002366 lys_set_implemented_internal((struct lys_module*)mod, 2);
Radek Krejci92cc8512019-04-25 09:57:06 +02002367 }
Radek Krejcia3045382018-11-22 14:30:31 +01002368 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002369 mod = start_node->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002370 }
Radek Krejcibade82a2018-12-05 10:13:48 +01002371 src_node = NULL;
Radek Krejci0fe9b512019-07-26 17:51:05 +02002372 if (!(context_node->flags & LYS_KEYLESS)) {
2373 struct lysc_node *key;
2374 for (key = context_node->child; key && key->nodetype == LYS_LEAF && (key->flags & LYS_KEY); key = key->next) {
Radek Krejci7f9b6512019-09-18 13:11:09 +02002375 if (!ly_strncmp(key->name, src, src_len)) {
Radek Krejci0fe9b512019-07-26 17:51:05 +02002376 src_node = key;
Radek Krejcibade82a2018-12-05 10:13:48 +01002377 break;
2378 }
2379 }
2380 }
Radek Krejcia3045382018-11-22 14:30:31 +01002381 if (!src_node) {
2382 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01002383 "Invalid leafref path predicate \"%.*s\" - predicate's key node \"%.*s\" not found.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002384 *predicate - start, start, src_len, src, mod->name);
Radek Krejcia3045382018-11-22 14:30:31 +01002385 goto cleanup;
2386 }
Radek Krejcia3045382018-11-22 14:30:31 +01002387
2388 /* check that there is only one predicate for the */
Radek Krejcibade82a2018-12-05 10:13:48 +01002389 c = keys.count;
Radek Krejcia3045382018-11-22 14:30:31 +01002390 i = ly_set_add(&keys, (void*)src_node, 0);
2391 LY_CHECK_GOTO(i == -1, cleanup);
Radek Krejcibade82a2018-12-05 10:13:48 +01002392 if (keys.count == c) { /* node was already present in the set */
Radek Krejcia3045382018-11-22 14:30:31 +01002393 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01002394 "Invalid leafref path predicate \"%.*s\" - multiple equality tests for the key \"%s\".",
Radek Krejcia3045382018-11-22 14:30:31 +01002395 *predicate - start, start, src_node->name);
2396 goto cleanup;
2397 }
2398
2399 /* destination */
2400 dest_parent_times = 0;
Radek Krejci4ce68932018-11-28 12:53:36 +01002401 dst_node = start_node;
Radek Krejcia3045382018-11-22 14:30:31 +01002402
2403 /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */
Radek Krejci7a3f89f2019-07-12 11:17:33 +02002404 if (strncmp(path_key_expr, "current", 7)) {
2405error_current_function_invocation:
Radek Krejcia3045382018-11-22 14:30:31 +01002406 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2407 "Invalid leafref path predicate \"%.*s\" - missing current-function-invocation.",
2408 *predicate - start, start);
2409 goto cleanup;
2410 }
Radek Krejci7a3f89f2019-07-12 11:17:33 +02002411 for (path_key_expr += 7; isspace(*path_key_expr); ++path_key_expr);
2412 if (*path_key_expr != '(') {
2413 goto error_current_function_invocation;
Radek Krejcia3045382018-11-22 14:30:31 +01002414 }
Radek Krejci7a3f89f2019-07-12 11:17:33 +02002415 for (path_key_expr++; isspace(*path_key_expr); ++path_key_expr);
2416 if (*path_key_expr != ')') {
2417 goto error_current_function_invocation;
2418 }
2419 for (path_key_expr++; isspace(*path_key_expr); ++path_key_expr);
Radek Krejcia3045382018-11-22 14:30:31 +01002420
2421 if (*path_key_expr != '/') {
2422 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2423 "Invalid leafref path predicate \"%.*s\" - missing \"/\" after current-function-invocation.",
2424 *predicate - start, start);
2425 goto cleanup;
2426 }
2427 ++path_key_expr;
2428 while (isspace(*path_key_expr)) {
2429 ++path_key_expr;
2430 }
2431
2432 /* rel-path-keyexpr:
2433 * 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier */
2434 while (!strncmp(path_key_expr, "..", 2)) {
2435 ++dest_parent_times;
2436 path_key_expr += 2;
2437 while (isspace(*path_key_expr)) {
2438 ++path_key_expr;
2439 }
2440 if (*path_key_expr != '/') {
2441 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2442 "Invalid leafref path predicate \"%.*s\" - missing \"/\" in \"../\" rel-path-keyexpr pattern.",
2443 *predicate - start, start);
2444 goto cleanup;
2445 }
2446 ++path_key_expr;
2447 while (isspace(*path_key_expr)) {
2448 ++path_key_expr;
2449 }
2450
2451 /* path is supposed to be evaluated in data tree, so we have to skip
2452 * all schema nodes that cannot be instantiated in data tree */
2453 MOVE_PATH_PARENT(dst_node, !strncmp(path_key_expr, "..", 2), goto cleanup,
2454 "Invalid leafref path predicate \"%.*s\" - too many \"..\" in rel-path-keyexpr.",
2455 *predicate - start, start);
2456 }
2457 if (!dest_parent_times) {
2458 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2459 "Invalid leafref path predicate \"%.*s\" - at least one \"..\" is expected in rel-path-keyexpr.",
2460 *predicate - start, start);
2461 goto cleanup;
2462 }
2463 if (path_key_expr == pke_end) {
2464 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2465 "Invalid leafref path predicate \"%.*s\" - at least one node-identifier is expected in rel-path-keyexpr.",
2466 *predicate - start, start);
2467 goto cleanup;
2468 }
2469
2470 while(path_key_expr != pke_end) {
Radek Krejci7a3f89f2019-07-12 11:17:33 +02002471 for (;*path_key_expr == '/' || isspace(*path_key_expr); ++path_key_expr);
Radek Krejcib4a4a272019-06-10 12:44:52 +02002472 if (ly_parse_nodeid(&path_key_expr, &dst_prefix, &dst_prefix_len, &dst, &dst_len)) {
Radek Krejcia3045382018-11-22 14:30:31 +01002473 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcibade82a2018-12-05 10:13:48 +01002474 "Invalid node identifier in leafref path predicate - character %d (of %.*s).",
2475 path_key_expr - start + 1, *predicate - start, start);
Radek Krejcia3045382018-11-22 14:30:31 +01002476 goto cleanup;
2477 }
2478
2479 if (dst_prefix) {
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002480 mod = lys_module_find_prefix(path_context, dst_prefix, dst_prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01002481 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002482 mod = start_node->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002483 }
2484 if (!mod) {
2485 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci92cc8512019-04-25 09:57:06 +02002486 "Invalid leafref path predicate \"%.*s\" - unable to find module of the node \"%.*s\" in rel-path-keyexpr.",
Radek Krejcia3045382018-11-22 14:30:31 +01002487 *predicate - start, start, dst_len, dst);
2488 goto cleanup;
2489 }
Radek Krejci92cc8512019-04-25 09:57:06 +02002490 if (!mod->implemented) {
2491 /* make the module implemented */
Radek Krejci77a8bcd2019-09-11 11:20:02 +02002492 lys_set_implemented_internal((struct lys_module*)mod, 2);
Radek Krejci92cc8512019-04-25 09:57:06 +02002493 }
Radek Krejcia3045382018-11-22 14:30:31 +01002494
2495 dst_node = lys_child(dst_node, mod, dst, dst_len, 0, LYS_GETNEXT_NOSTATECHECK);
2496 if (!dst_node) {
2497 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci92cc8512019-04-25 09:57:06 +02002498 "Invalid leafref path predicate \"%.*s\" - unable to find node \"%.*s\" in the rel-path-keyexpr.",
Radek Krejcia3045382018-11-22 14:30:31 +01002499 *predicate - start, start, path_key_expr - pke_start, pke_start);
2500 goto cleanup;
2501 }
2502 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002503 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 +01002504 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci92cc8512019-04-25 09:57:06 +02002505 "Invalid leafref path predicate \"%.*s\" - rel-path-keyexpr \"%.*s\" refers %s instead of leaf.",
Radek Krejcia3045382018-11-22 14:30:31 +01002506 *predicate - start, start, path_key_expr - pke_start, pke_start, lys_nodetype2str(dst_node->nodetype));
2507 goto cleanup;
2508 }
2509 }
2510
2511 ret = LY_SUCCESS;
2512cleanup:
2513 ly_set_erase(&keys, NULL);
2514 return ret;
2515}
2516
2517/**
2518 * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function.
2519 *
2520 * path-arg = absolute-path / relative-path
2521 * absolute-path = 1*("/" (node-identifier *path-predicate))
2522 * relative-path = 1*(".." "/") descendant-path
2523 *
2524 * @param[in,out] path Path to parse.
2525 * @param[out] prefix Prefix of the token, NULL if there is not any.
2526 * @param[out] pref_len Length of the prefix, 0 if there is not any.
2527 * @param[out] name Name of the token.
2528 * @param[out] nam_len Length of the name.
2529 * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call,
2530 * must not be changed between consecutive calls. -1 if the
2531 * path is absolute.
2532 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
2533 * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path.
2534 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002535LY_ERR
Radek Krejcia3045382018-11-22 14:30:31 +01002536lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len,
2537 int *parent_times, int *has_predicate)
2538{
2539 int par_times = 0;
2540
2541 assert(path && *path);
2542 assert(parent_times);
2543 assert(prefix);
2544 assert(prefix_len);
2545 assert(name);
2546 assert(name_len);
2547 assert(has_predicate);
2548
2549 *prefix = NULL;
2550 *prefix_len = 0;
2551 *name = NULL;
2552 *name_len = 0;
2553 *has_predicate = 0;
2554
2555 if (!*parent_times) {
2556 if (!strncmp(*path, "..", 2)) {
2557 *path += 2;
2558 ++par_times;
2559 while (!strncmp(*path, "/..", 3)) {
2560 *path += 3;
2561 ++par_times;
2562 }
2563 }
2564 if (par_times) {
2565 *parent_times = par_times;
2566 } else {
2567 *parent_times = -1;
2568 }
2569 }
2570
2571 if (**path != '/') {
2572 return LY_EINVAL;
2573 }
2574 /* skip '/' */
2575 ++(*path);
2576
2577 /* node-identifier ([prefix:]name) */
Radek Krejcib4a4a272019-06-10 12:44:52 +02002578 LY_CHECK_RET(ly_parse_nodeid(path, prefix, prefix_len, name, name_len));
Radek Krejcia3045382018-11-22 14:30:31 +01002579
2580 if ((**path == '/' && (*path)[1]) || !**path) {
2581 /* path continues by another token or this is the last token */
2582 return LY_SUCCESS;
2583 } else if ((*path)[0] != '[') {
2584 /* unexpected character */
2585 return LY_EINVAL;
2586 } else {
2587 /* predicate starting with [ */
2588 *has_predicate = 1;
2589 return LY_SUCCESS;
2590 }
2591}
2592
2593/**
Radek Krejci58d171e2018-11-23 13:50:55 +01002594 * @brief Check the features used in if-feature statements applicable to the leafref and its target.
2595 *
2596 * The set of features used for target must be a subset of features used for the leafref.
2597 * This is not a perfect, we should compare the truth tables but it could require too much resources
2598 * and RFC 7950 does not require it explicitely, so we simplify that.
2599 *
2600 * @param[in] refnode The leafref node.
2601 * @param[in] target Tha target node of the leafref.
2602 * @return LY_SUCCESS or LY_EVALID;
2603 */
2604static LY_ERR
2605lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target)
2606{
2607 LY_ERR ret = LY_EVALID;
2608 const struct lysc_node *iter;
Radek Krejci58d171e2018-11-23 13:50:55 +01002609 unsigned int u, v, count;
2610 struct ly_set features = {0};
2611
2612 for (iter = refnode; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002613 if (iter->iffeatures) {
2614 LY_ARRAY_FOR(iter->iffeatures, u) {
2615 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2616 LY_CHECK_GOTO(ly_set_add(&features, iter->iffeatures[u].features[v], 0) == -1, cleanup);
Radek Krejci58d171e2018-11-23 13:50:55 +01002617 }
2618 }
2619 }
2620 }
2621
2622 /* we should have, in features set, a superset of features applicable to the target node.
2623 * So when adding features applicable to the target into the features set, we should not be
2624 * able to actually add any new feature, otherwise it is not a subset of features applicable
2625 * to the leafref itself. */
2626 count = features.count;
2627 for (iter = target; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002628 if (iter->iffeatures) {
2629 LY_ARRAY_FOR(iter->iffeatures, u) {
2630 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2631 if ((unsigned int)ly_set_add(&features, iter->iffeatures[u].features[v], 0) >= count) {
Radek Krejci58d171e2018-11-23 13:50:55 +01002632 /* new feature was added (or LY_EMEM) */
2633 goto cleanup;
2634 }
2635 }
2636 }
2637 }
2638 }
2639 ret = LY_SUCCESS;
2640
2641cleanup:
2642 ly_set_erase(&features, NULL);
2643 return ret;
2644}
2645
2646/**
Radek Krejcia3045382018-11-22 14:30:31 +01002647 * @brief Validate the leafref path.
2648 * @param[in] ctx Compile context
2649 * @param[in] startnode Path context node (where the leafref path begins/is placed).
Radek Krejci412ddfa2018-11-23 11:44:11 +01002650 * @param[in] leafref Leafref to validate.
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02002651 * @param[out] target Optional resolved leafref target.
Radek Krejcia3045382018-11-22 14:30:31 +01002652 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2653 */
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02002654LY_ERR
2655lys_compile_leafref_validate(struct lysc_ctx *ctx, struct lysc_node *startnode, struct lysc_type_leafref *leafref,
2656 const struct lysc_node **target)
Radek Krejcia3045382018-11-22 14:30:31 +01002657{
Michal Vaskoecd62de2019-11-13 12:35:11 +01002658 const struct lysc_node *node = NULL, *parent = NULL;
Radek Krejcia3045382018-11-22 14:30:31 +01002659 const struct lys_module *mod;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002660 struct lysc_type *type;
Radek Krejcia3045382018-11-22 14:30:31 +01002661 const char *id, *prefix, *name;
2662 size_t prefix_len, name_len;
2663 int parent_times = 0, has_predicate;
2664 unsigned int iter, u;
2665 LY_ERR ret = LY_SUCCESS;
2666
2667 assert(ctx);
2668 assert(startnode);
Radek Krejci412ddfa2018-11-23 11:44:11 +01002669 assert(leafref);
2670
Radek Krejci1c0c3442019-07-23 16:08:47 +02002671 ctx->path[0] = '\0';
Michal Vasko03ff5a72019-09-11 13:49:33 +02002672 lysc_path(startnode, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
Radek Krejci1c0c3442019-07-23 16:08:47 +02002673 ctx->path_len = strlen(ctx->path);
Radek Krejci327de162019-06-14 12:52:07 +02002674
Radek Krejcia3045382018-11-22 14:30:31 +01002675 iter = 0;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002676 id = leafref->path;
Radek Krejcia3045382018-11-22 14:30:31 +01002677 while(*id && (ret = lys_path_token(&id, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)) == LY_SUCCESS) {
2678 if (!iter) { /* first iteration */
2679 /* precess ".." in relative paths */
2680 if (parent_times > 0) {
2681 /* move from the context node */
2682 for (u = 0, parent = startnode; u < (unsigned int)parent_times; u++) {
2683 /* path is supposed to be evaluated in data tree, so we have to skip
2684 * all schema nodes that cannot be instantiated in data tree */
2685 MOVE_PATH_PARENT(parent, u < (unsigned int)parent_times - 1, return LY_EVALID,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002686 "Invalid leafref path \"%s\" - too many \"..\" in the path.", leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002687 }
2688 }
2689 }
2690
2691 if (prefix) {
Radek Krejci412ddfa2018-11-23 11:44:11 +01002692 mod = lys_module_find_prefix(leafref->path_context, prefix, prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01002693 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002694 mod = startnode->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002695 }
2696 if (!mod) {
2697 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002698 "Invalid leafref path - unable to find module connected with the prefix of the node \"%.*s\".",
2699 id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002700 return LY_EVALID;
2701 }
Radek Krejci3d929562019-02-21 11:25:55 +01002702 if (!mod->implemented) {
2703 /* make the module implemented */
Radek Krejci77a8bcd2019-09-11 11:20:02 +02002704 lys_set_implemented_internal((struct lys_module*)mod, 2);
Radek Krejci3d929562019-02-21 11:25:55 +01002705 }
Radek Krejcia3045382018-11-22 14:30:31 +01002706
2707 node = lys_child(parent, mod, name, name_len, 0, LYS_GETNEXT_NOSTATECHECK);
2708 if (!node) {
2709 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002710 "Invalid leafref path - unable to find \"%.*s\".", id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002711 return LY_EVALID;
2712 }
2713 parent = node;
2714
2715 if (has_predicate) {
2716 /* we have predicate, so the current result must be list */
2717 if (node->nodetype != LYS_LIST) {
2718 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2719 "Invalid leafref path - node \"%.*s\" is expected to be a list, but it is %s.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01002720 id - leafref->path, leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01002721 return LY_EVALID;
2722 }
2723
Radek Krejcibade82a2018-12-05 10:13:48 +01002724 LY_CHECK_RET(lys_compile_leafref_predicate_validate(ctx, &id, startnode, (struct lysc_node_list*)node, leafref->path_context),
2725 LY_EVALID);
Radek Krejcia3045382018-11-22 14:30:31 +01002726 }
2727
2728 ++iter;
2729 }
2730 if (ret) {
2731 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002732 "Invalid leafref path at character %d (%s).", id - leafref->path + 1, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002733 return LY_EVALID;
2734 }
2735
2736 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
2737 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2738 "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01002739 leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01002740 return LY_EVALID;
2741 }
2742
2743 /* check status */
2744 if (lysc_check_status(ctx, startnode->flags, startnode->module, startnode->name, node->flags, node->module, node->name)) {
2745 return LY_EVALID;
2746 }
2747
Radek Krejcib57cf1e2018-11-23 14:07:05 +01002748 /* check config */
2749 if (leafref->require_instance && (startnode->flags & LYS_CONFIG_W)) {
2750 if (node->flags & LYS_CONFIG_R) {
2751 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2752 "Invalid leafref path \"%s\" - target is supposed to represent configuration data (as the leafref does), but it does not.",
2753 leafref->path);
2754 return LY_EVALID;
2755 }
2756 }
2757
Radek Krejci412ddfa2018-11-23 11:44:11 +01002758 /* store the target's type and check for circular chain of leafrefs */
2759 leafref->realtype = ((struct lysc_node_leaf*)node)->type;
2760 for (type = leafref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref*)type)->realtype) {
2761 if (type == (struct lysc_type*)leafref) {
2762 /* circular chain detected */
2763 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2764 "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", leafref->path);
2765 return LY_EVALID;
2766 }
2767 }
2768
Radek Krejci58d171e2018-11-23 13:50:55 +01002769 /* check if leafref and its target are under common if-features */
2770 if (lys_compile_leafref_features_validate(startnode, node)) {
2771 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2772 "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of features applicable to the leafref itself.",
2773 leafref->path);
2774 return LY_EVALID;
2775 }
2776
Radek Krejci327de162019-06-14 12:52:07 +02002777 ctx->path_len = 1;
2778 ctx->path[1] = '\0';
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02002779 if (target) {
2780 *target = node;
2781 }
Radek Krejcia3045382018-11-22 14:30:31 +01002782 return LY_SUCCESS;
2783}
2784
Radek Krejcicdfecd92018-11-26 11:27:32 +01002785static 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 +02002786 struct lysp_type *type_p, struct lysc_type **type, const char **units);
Radek Krejcia3045382018-11-22 14:30:31 +01002787/**
2788 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
2789 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002790 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2791 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2792 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2793 * @param[in] context_name Name of the context node or referencing typedef for logging.
Radek Krejcia3045382018-11-22 14:30:31 +01002794 * @param[in] type_p Parsed type to compile.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002795 * @param[in] module Context module for the leafref path (to correctly resolve prefixes in path)
Radek Krejcia3045382018-11-22 14:30:31 +01002796 * @param[in] basetype Base YANG built-in type of the type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002797 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
2798 * @param[in] base The latest base (compiled) type from which the current type is being derived.
2799 * @param[out] type Newly created type structure with the filled information about the type.
2800 * @return LY_ERR value.
2801 */
Radek Krejci19a96102018-11-15 13:38:09 +01002802static LY_ERR
Radek Krejcicdfecd92018-11-26 11:27:32 +01002803lys_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 +02002804 struct lysp_type *type_p, struct lys_module *module, LY_DATA_TYPE basetype, const char *tpdfname,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002805 struct lysc_type *base, struct lysc_type **type)
Radek Krejcic5c27e52018-11-15 14:38:11 +01002806{
2807 LY_ERR ret = LY_SUCCESS;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002808 unsigned int u, v, additional;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002809 struct lysc_type_bin *bin;
2810 struct lysc_type_num *num;
2811 struct lysc_type_str *str;
2812 struct lysc_type_bits *bits;
2813 struct lysc_type_enum *enumeration;
Radek Krejci6cba4292018-11-15 17:33:29 +01002814 struct lysc_type_dec *dec;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002815 struct lysc_type_identityref *idref;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002816 struct lysc_type_union *un, *un_aux;
2817 void *p;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002818
2819 switch (basetype) {
2820 case LY_TYPE_BINARY:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002821 bin = (struct lysc_type_bin*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002822
2823 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002824 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002825 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
2826 base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002827 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002828 COMPILE_EXTS_GOTO(ctx, type_p->length->exts, bin->length->exts, bin->length, LYEXT_PAR_LENGTH, ret, done);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002829 }
2830 }
2831
2832 if (tpdfname) {
2833 type_p->compiled = *type;
2834 *type = calloc(1, sizeof(struct lysc_type_bin));
2835 }
2836 break;
2837 case LY_TYPE_BITS:
2838 /* RFC 7950 9.7 - bits */
2839 bits = (struct lysc_type_bits*)(*type);
2840 if (type_p->bits) {
Radek Krejciec4da802019-05-02 13:02:41 +02002841 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002842 base ? (struct lysc_type_bitenum_item*)((struct lysc_type_bits*)base)->bits : NULL,
2843 (struct lysc_type_bitenum_item**)&bits->bits));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002844 }
2845
Radek Krejci555cb5b2018-11-16 14:54:33 +01002846 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002847 /* type derived from bits built-in type must contain at least one bit */
Radek Krejci6cba4292018-11-15 17:33:29 +01002848 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002849 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002850 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002851 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002852 free(*type);
2853 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002854 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002855 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002856 }
2857
2858 if (tpdfname) {
2859 type_p->compiled = *type;
2860 *type = calloc(1, sizeof(struct lysc_type_bits));
2861 }
2862 break;
Radek Krejci6cba4292018-11-15 17:33:29 +01002863 case LY_TYPE_DEC64:
2864 dec = (struct lysc_type_dec*)(*type);
2865
2866 /* RFC 7950 9.3.4 - fraction-digits */
Radek Krejci555cb5b2018-11-16 14:54:33 +01002867 if (!base) {
Radek Krejci643c8242018-11-15 17:51:11 +01002868 if (!type_p->fraction_digits) {
2869 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002870 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Radek Krejci643c8242018-11-15 17:51:11 +01002871 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002872 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Radek Krejci643c8242018-11-15 17:51:11 +01002873 free(*type);
2874 *type = NULL;
2875 }
2876 return LY_EVALID;
2877 }
2878 } else if (type_p->fraction_digits) {
2879 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
Radek Krejci6cba4292018-11-15 17:33:29 +01002880 if (tpdfname) {
Radek Krejci643c8242018-11-15 17:51:11 +01002881 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2882 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
Radek Krejci6cba4292018-11-15 17:33:29 +01002883 tpdfname);
2884 } else {
Radek Krejci643c8242018-11-15 17:51:11 +01002885 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2886 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
Radek Krejci6cba4292018-11-15 17:33:29 +01002887 free(*type);
2888 *type = NULL;
2889 }
2890 return LY_EVALID;
2891 }
2892 dec->fraction_digits = type_p->fraction_digits;
2893
2894 /* RFC 7950 9.2.4 - range */
2895 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002896 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
2897 base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range));
Radek Krejci6cba4292018-11-15 17:33:29 +01002898 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002899 COMPILE_EXTS_GOTO(ctx, type_p->range->exts, dec->range->exts, dec->range, LYEXT_PAR_RANGE, ret, done);
Radek Krejci6cba4292018-11-15 17:33:29 +01002900 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002901 }
2902
2903 if (tpdfname) {
2904 type_p->compiled = *type;
2905 *type = calloc(1, sizeof(struct lysc_type_dec));
2906 }
2907 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002908 case LY_TYPE_STRING:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002909 str = (struct lysc_type_str*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002910
2911 /* RFC 7950 9.4.4 - length */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002912 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002913 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
2914 base ? ((struct lysc_type_str*)base)->length : NULL, &str->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002915 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002916 COMPILE_EXTS_GOTO(ctx, type_p->length->exts, str->length->exts, str->length, LYEXT_PAR_LENGTH, ret, done);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002917 }
2918 } else if (base && ((struct lysc_type_str*)base)->length) {
2919 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length);
2920 }
2921
2922 /* RFC 7950 9.4.5 - pattern */
2923 if (type_p->patterns) {
Radek Krejciec4da802019-05-02 13:02:41 +02002924 LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002925 base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002926 } else if (base && ((struct lysc_type_str*)base)->patterns) {
2927 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns);
2928 }
2929
2930 if (tpdfname) {
2931 type_p->compiled = *type;
2932 *type = calloc(1, sizeof(struct lysc_type_str));
2933 }
2934 break;
2935 case LY_TYPE_ENUM:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002936 enumeration = (struct lysc_type_enum*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002937
2938 /* RFC 7950 9.6 - enum */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002939 if (type_p->enums) {
Radek Krejciec4da802019-05-02 13:02:41 +02002940 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002941 base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002942 }
2943
Radek Krejci555cb5b2018-11-16 14:54:33 +01002944 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002945 /* type derived from enumerations built-in type must contain at least one enum */
Radek Krejci6cba4292018-11-15 17:33:29 +01002946 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002947 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002948 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002949 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002950 free(*type);
2951 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002952 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002953 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002954 }
2955
2956 if (tpdfname) {
2957 type_p->compiled = *type;
2958 *type = calloc(1, sizeof(struct lysc_type_enum));
2959 }
2960 break;
2961 case LY_TYPE_INT8:
2962 case LY_TYPE_UINT8:
2963 case LY_TYPE_INT16:
2964 case LY_TYPE_UINT16:
2965 case LY_TYPE_INT32:
2966 case LY_TYPE_UINT32:
2967 case LY_TYPE_INT64:
2968 case LY_TYPE_UINT64:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002969 num = (struct lysc_type_num*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002970
2971 /* RFC 6020 9.2.4 - range */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002972 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002973 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
2974 base ? ((struct lysc_type_num*)base)->range : NULL, &num->range));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002975 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002976 COMPILE_EXTS_GOTO(ctx, type_p->range->exts, num->range->exts, num->range, LYEXT_PAR_RANGE, ret, done);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002977 }
2978 }
2979
2980 if (tpdfname) {
2981 type_p->compiled = *type;
2982 *type = calloc(1, sizeof(struct lysc_type_num));
2983 }
2984 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002985 case LY_TYPE_IDENT:
2986 idref = (struct lysc_type_identityref*)(*type);
2987
2988 /* RFC 7950 9.10.2 - base */
2989 if (type_p->bases) {
2990 if (base) {
2991 /* only the directly derived identityrefs can contain base specification */
2992 if (tpdfname) {
2993 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002994 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
Radek Krejci555cb5b2018-11-16 14:54:33 +01002995 tpdfname);
2996 } else {
2997 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002998 "Invalid base substatement for the type not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01002999 free(*type);
3000 *type = NULL;
3001 }
3002 return LY_EVALID;
3003 }
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003004 LY_CHECK_RET(lys_compile_identity_bases(ctx, type_p->bases, NULL, &idref->bases));
Radek Krejci555cb5b2018-11-16 14:54:33 +01003005 }
3006
3007 if (!base && !type_p->flags) {
3008 /* type derived from identityref built-in type must contain at least one base */
3009 if (tpdfname) {
3010 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
3011 } else {
3012 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
3013 free(*type);
3014 *type = NULL;
3015 }
3016 return LY_EVALID;
3017 }
3018
3019 if (tpdfname) {
3020 type_p->compiled = *type;
3021 *type = calloc(1, sizeof(struct lysc_type_identityref));
3022 }
3023 break;
Radek Krejcia3045382018-11-22 14:30:31 +01003024 case LY_TYPE_LEAFREF:
3025 /* RFC 7950 9.9.3 - require-instance */
3026 if (type_p->flags & LYS_SET_REQINST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003027 if (context_mod->mod->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003028 if (tpdfname) {
3029 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3030 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
3031 } else {
3032 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3033 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
3034 free(*type);
3035 *type = NULL;
3036 }
3037 return LY_EVALID;
3038 }
Radek Krejcia3045382018-11-22 14:30:31 +01003039 ((struct lysc_type_leafref*)(*type))->require_instance = type_p->require_instance;
Radek Krejci412ddfa2018-11-23 11:44:11 +01003040 } else if (base) {
3041 /* inherit */
3042 ((struct lysc_type_leafref*)(*type))->require_instance = ((struct lysc_type_leafref*)base)->require_instance;
Radek Krejcia3045382018-11-22 14:30:31 +01003043 } else {
3044 /* default is true */
3045 ((struct lysc_type_leafref*)(*type))->require_instance = 1;
3046 }
3047 if (type_p->path) {
3048 DUP_STRING(ctx->ctx, (void*)type_p->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01003049 ((struct lysc_type_leafref*)(*type))->path_context = module;
Radek Krejcia3045382018-11-22 14:30:31 +01003050 } else if (base) {
3051 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)base)->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01003052 ((struct lysc_type_leafref*)(*type))->path_context = ((struct lysc_type_leafref*)base)->path_context;
Radek Krejcia3045382018-11-22 14:30:31 +01003053 } else if (tpdfname) {
3054 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
3055 return LY_EVALID;
3056 } else {
3057 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
3058 free(*type);
3059 *type = NULL;
3060 return LY_EVALID;
3061 }
3062 if (tpdfname) {
3063 type_p->compiled = *type;
3064 *type = calloc(1, sizeof(struct lysc_type_leafref));
3065 }
3066 break;
Radek Krejci16c0f822018-11-16 10:46:10 +01003067 case LY_TYPE_INST:
3068 /* RFC 7950 9.9.3 - require-instance */
3069 if (type_p->flags & LYS_SET_REQINST) {
3070 ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance;
3071 } else {
3072 /* default is true */
3073 ((struct lysc_type_instanceid*)(*type))->require_instance = 1;
3074 }
3075
3076 if (tpdfname) {
3077 type_p->compiled = *type;
3078 *type = calloc(1, sizeof(struct lysc_type_instanceid));
3079 }
3080 break;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003081 case LY_TYPE_UNION:
3082 un = (struct lysc_type_union*)(*type);
3083
3084 /* RFC 7950 7.4 - type */
3085 if (type_p->types) {
3086 if (base) {
3087 /* only the directly derived union can contain types specification */
3088 if (tpdfname) {
3089 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
3090 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
3091 tpdfname);
3092 } else {
3093 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
3094 "Invalid type substatement for the type not directly derived from union built-in type.");
3095 free(*type);
3096 *type = NULL;
3097 }
3098 return LY_EVALID;
3099 }
3100 /* compile the type */
3101 additional = 0;
3102 LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_SIZE(type_p->types), LY_EVALID);
3103 for (u = 0; u < LY_ARRAY_SIZE(type_p->types); ++u) {
Radek Krejciec4da802019-05-02 13:02:41 +02003104 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 +01003105 if (un->types[u + additional]->basetype == LY_TYPE_UNION) {
3106 /* add space for additional types from the union subtype */
3107 un_aux = (struct lysc_type_union *)un->types[u + additional];
3108 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)));
3109 LY_CHECK_ERR_RET(!p, LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
3110 un->types = (void*)((uint32_t*)(p) + 1);
3111
3112 /* copy subtypes of the subtype union */
3113 for (v = 0; v < LY_ARRAY_SIZE(un_aux->types); ++v) {
3114 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
3115 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
3116 un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
3117 LY_CHECK_ERR_RET(!un->types[u + additional], LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
3118 ((struct lysc_type_leafref*)un->types[u + additional])->basetype = LY_TYPE_LEAFREF;
3119 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)un_aux->types[v])->path, ((struct lysc_type_leafref*)un->types[u + additional])->path);
3120 ((struct lysc_type_leafref*)un->types[u + additional])->refcount = 1;
3121 ((struct lysc_type_leafref*)un->types[u + additional])->require_instance = ((struct lysc_type_leafref*)un_aux->types[v])->require_instance;
3122 ((struct lysc_type_leafref*)un->types[u + additional])->path_context = ((struct lysc_type_leafref*)un_aux->types[v])->path_context;
3123 /* TODO extensions */
3124
3125 } else {
3126 un->types[u + additional] = un_aux->types[v];
3127 ++un_aux->types[v]->refcount;
3128 }
3129 ++additional;
3130 LY_ARRAY_INCREMENT(un->types);
3131 }
3132 /* compensate u increment in main loop */
3133 --additional;
3134
3135 /* free the replaced union subtype */
3136 lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux);
3137 } else {
3138 LY_ARRAY_INCREMENT(un->types);
3139 }
Radek Krejcicdfecd92018-11-26 11:27:32 +01003140 }
3141 }
3142
3143 if (!base && !type_p->flags) {
3144 /* type derived from union built-in type must contain at least one type */
3145 if (tpdfname) {
3146 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
3147 } else {
3148 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
3149 free(*type);
3150 *type = NULL;
3151 }
3152 return LY_EVALID;
3153 }
3154
3155 if (tpdfname) {
3156 type_p->compiled = *type;
3157 *type = calloc(1, sizeof(struct lysc_type_union));
3158 }
3159 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01003160 case LY_TYPE_BOOL:
3161 case LY_TYPE_EMPTY:
3162 case LY_TYPE_UNKNOWN: /* just to complete switch */
3163 break;
3164 }
3165 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
3166done:
3167 return ret;
3168}
3169
Radek Krejcia3045382018-11-22 14:30:31 +01003170/**
3171 * @brief Compile information about the leaf/leaf-list's type.
3172 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01003173 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
3174 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
3175 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
3176 * @param[in] context_name Name of the context node or referencing typedef for logging.
3177 * @param[in] type_p Parsed type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01003178 * @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 +01003179 * @param[out] units Storage for inheriting units value from the typedefs the current type derives from.
Radek Krejcia3045382018-11-22 14:30:31 +01003180 * @return LY_ERR value.
3181 */
Radek Krejcic5c27e52018-11-15 14:38:11 +01003182static LY_ERR
Radek Krejcicdfecd92018-11-26 11:27:32 +01003183lys_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 +02003184 struct lysp_type *type_p, struct lysc_type **type, const char **units)
Radek Krejci19a96102018-11-15 13:38:09 +01003185{
3186 LY_ERR ret = LY_SUCCESS;
3187 unsigned int u;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003188 int dummyloops = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01003189 struct type_context {
3190 const struct lysp_tpdf *tpdf;
3191 struct lysp_node *node;
3192 struct lysp_module *mod;
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003193 } *tctx, *tctx_prev = NULL, *tctx_iter;
Radek Krejci19a96102018-11-15 13:38:09 +01003194 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
Radek Krejcic5c27e52018-11-15 14:38:11 +01003195 struct lysc_type *base = NULL, *prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01003196 struct ly_set tpdf_chain = {0};
Radek Krejci01342af2019-01-03 15:18:08 +01003197 const char *dflt = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02003198 struct lys_module *dflt_mod = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +01003199
3200 (*type) = NULL;
3201
3202 tctx = calloc(1, sizeof *tctx);
3203 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +01003204 for (ret = lysp_type_find(type_p->name, context_node_p, ctx->mod_def->parsed,
Radek Krejci19a96102018-11-15 13:38:09 +01003205 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod);
3206 ret == LY_SUCCESS;
3207 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod,
3208 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) {
3209 if (basetype) {
3210 break;
3211 }
3212
3213 /* check status */
Radek Krejcicdfecd92018-11-26 11:27:32 +01003214 ret = lysc_check_status(ctx, context_flags, context_mod, context_name,
3215 tctx->tpdf->flags, tctx->mod, tctx->node ? tctx->node->name : tctx->tpdf->name);
Radek Krejci19a96102018-11-15 13:38:09 +01003216 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
3217
Radek Krejcicdfecd92018-11-26 11:27:32 +01003218 if (units && !*units) {
3219 /* inherit units */
3220 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units);
3221 }
Radek Krejci01342af2019-01-03 15:18:08 +01003222 if (!dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01003223 /* inherit default */
Radek Krejci01342af2019-01-03 15:18:08 +01003224 dflt = tctx->tpdf->dflt;
Radek Krejcia1911222019-07-22 17:24:50 +02003225 dflt_mod = tctx->mod->mod;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003226 }
Radek Krejci01342af2019-01-03 15:18:08 +01003227 if (dummyloops && (!units || *units) && dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01003228 basetype = ((struct type_context*)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
3229 break;
3230 }
3231
Radek Krejci19a96102018-11-15 13:38:09 +01003232 if (tctx->tpdf->type.compiled) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01003233 /* it is not necessary to continue, the rest of the chain was already compiled,
3234 * but we still may need to inherit default and units values, so start dummy loops */
Radek Krejci19a96102018-11-15 13:38:09 +01003235 basetype = tctx->tpdf->type.compiled->basetype;
3236 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
Radek Krejci01342af2019-01-03 15:18:08 +01003237 if ((units && !*units) || !dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01003238 dummyloops = 1;
3239 goto preparenext;
3240 } else {
3241 tctx = NULL;
3242 break;
3243 }
Radek Krejci19a96102018-11-15 13:38:09 +01003244 }
3245
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003246 /* circular typedef reference detection */
3247 for (u = 0; u < tpdf_chain.count; u++) {
3248 /* local part */
3249 tctx_iter = (struct type_context*)tpdf_chain.objs[u];
3250 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
3251 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3252 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
3253 free(tctx);
3254 ret = LY_EVALID;
3255 goto cleanup;
3256 }
3257 }
3258 for (u = 0; u < ctx->tpdf_chain.count; u++) {
3259 /* global part for unions corner case */
3260 tctx_iter = (struct type_context*)ctx->tpdf_chain.objs[u];
3261 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
3262 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3263 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
3264 free(tctx);
3265 ret = LY_EVALID;
3266 goto cleanup;
3267 }
3268 }
3269
Radek Krejci19a96102018-11-15 13:38:09 +01003270 /* store information for the following processing */
3271 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
3272
Radek Krejcicdfecd92018-11-26 11:27:32 +01003273preparenext:
Radek Krejci19a96102018-11-15 13:38:09 +01003274 /* prepare next loop */
3275 tctx_prev = tctx;
3276 tctx = calloc(1, sizeof *tctx);
3277 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
3278 }
3279 free(tctx);
3280
3281 /* allocate type according to the basetype */
3282 switch (basetype) {
3283 case LY_TYPE_BINARY:
3284 *type = calloc(1, sizeof(struct lysc_type_bin));
Radek Krejci19a96102018-11-15 13:38:09 +01003285 break;
3286 case LY_TYPE_BITS:
3287 *type = calloc(1, sizeof(struct lysc_type_bits));
Radek Krejci19a96102018-11-15 13:38:09 +01003288 break;
3289 case LY_TYPE_BOOL:
3290 case LY_TYPE_EMPTY:
3291 *type = calloc(1, sizeof(struct lysc_type));
3292 break;
3293 case LY_TYPE_DEC64:
3294 *type = calloc(1, sizeof(struct lysc_type_dec));
3295 break;
3296 case LY_TYPE_ENUM:
3297 *type = calloc(1, sizeof(struct lysc_type_enum));
Radek Krejci19a96102018-11-15 13:38:09 +01003298 break;
3299 case LY_TYPE_IDENT:
3300 *type = calloc(1, sizeof(struct lysc_type_identityref));
3301 break;
3302 case LY_TYPE_INST:
3303 *type = calloc(1, sizeof(struct lysc_type_instanceid));
3304 break;
3305 case LY_TYPE_LEAFREF:
3306 *type = calloc(1, sizeof(struct lysc_type_leafref));
3307 break;
3308 case LY_TYPE_STRING:
3309 *type = calloc(1, sizeof(struct lysc_type_str));
Radek Krejci19a96102018-11-15 13:38:09 +01003310 break;
3311 case LY_TYPE_UNION:
3312 *type = calloc(1, sizeof(struct lysc_type_union));
3313 break;
3314 case LY_TYPE_INT8:
3315 case LY_TYPE_UINT8:
3316 case LY_TYPE_INT16:
3317 case LY_TYPE_UINT16:
3318 case LY_TYPE_INT32:
3319 case LY_TYPE_UINT32:
3320 case LY_TYPE_INT64:
3321 case LY_TYPE_UINT64:
3322 *type = calloc(1, sizeof(struct lysc_type_num));
Radek Krejci19a96102018-11-15 13:38:09 +01003323 break;
3324 case LY_TYPE_UNKNOWN:
3325 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3326 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
3327 ret = LY_EVALID;
3328 goto cleanup;
3329 }
3330 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
Radek Krejcicdfecd92018-11-26 11:27:32 +01003331 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci19a96102018-11-15 13:38:09 +01003332 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
3333 ly_data_type2str[basetype]);
3334 free(*type);
3335 (*type) = NULL;
3336 ret = LY_EVALID;
3337 goto cleanup;
3338 }
3339
3340 /* get restrictions from the referred typedefs */
3341 for (u = tpdf_chain.count - 1; u + 1 > 0; --u) {
3342 tctx = (struct type_context*)tpdf_chain.objs[u];
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003343
3344 /* remember the typedef context for circular check */
3345 ly_set_add(&ctx->tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
3346
Radek Krejci43699232018-11-23 14:59:46 +01003347 if (tctx->tpdf->type.compiled) {
Radek Krejci19a96102018-11-15 13:38:09 +01003348 base = tctx->tpdf->type.compiled;
3349 continue;
Radek Krejci43699232018-11-23 14:59:46 +01003350 } else if (basetype != LY_TYPE_LEAFREF && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) {
Radek Krejci19a96102018-11-15 13:38:09 +01003351 /* no change, just use the type information from the base */
3352 base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled;
3353 ++base->refcount;
3354 continue;
3355 }
3356
3357 ++(*type)->refcount;
Radek Krejci43699232018-11-23 14:59:46 +01003358 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
3359 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
3360 tctx->tpdf->name, ly_data_type2str[basetype]);
3361 ret = LY_EVALID;
3362 goto cleanup;
3363 } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) {
3364 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3365 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
3366 tctx->tpdf->name, tctx->tpdf->dflt);
3367 ret = LY_EVALID;
3368 goto cleanup;
3369 }
3370
Radek Krejci19a96102018-11-15 13:38:09 +01003371 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003372 /* TODO user type plugins */
3373 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejcic5c27e52018-11-15 14:38:11 +01003374 prev_type = *type;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003375 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 +01003376 basetype & (LY_TYPE_LEAFREF | LY_TYPE_UNION) ? lysp_find_module(ctx->ctx, tctx->mod) : NULL,
Radek Krejciec4da802019-05-02 13:02:41 +02003377 basetype, tctx->tpdf->name, base, type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01003378 LY_CHECK_GOTO(ret, cleanup);
3379 base = prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01003380 }
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003381 /* remove the processed typedef contexts from the stack for circular check */
3382 ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count;
Radek Krejci19a96102018-11-15 13:38:09 +01003383
Radek Krejcic5c27e52018-11-15 14:38:11 +01003384 /* process the type definition in leaf */
Radek Krejcicdfecd92018-11-26 11:27:32 +01003385 if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) {
Radek Krejcia3045382018-11-22 14:30:31 +01003386 /* get restrictions from the node itself */
Radek Krejci19a96102018-11-15 13:38:09 +01003387 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003388 /* TODO user type plugins */
3389 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejci19a96102018-11-15 13:38:09 +01003390 ++(*type)->refcount;
Radek Krejciec4da802019-05-02 13:02:41 +02003391 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 +01003392 LY_CHECK_GOTO(ret, cleanup);
3393 } else {
Radek Krejci19a96102018-11-15 13:38:09 +01003394 /* no specific restriction in leaf's type definition, copy from the base */
3395 free(*type);
3396 (*type) = base;
3397 ++(*type)->refcount;
Radek Krejci19a96102018-11-15 13:38:09 +01003398 }
Radek Krejcia1911222019-07-22 17:24:50 +02003399 if (dflt && !(*type)->dflt) {
3400 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003401 (*type)->dflt_mod = dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02003402 (*type)->dflt = calloc(1, sizeof *(*type)->dflt);
3403 (*type)->dflt->realtype = (*type);
3404 ret = (*type)->plugin->store(ctx->ctx, (*type), dflt, strlen(dflt),
3405 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003406 lys_resolve_prefix, (void*)(*type)->dflt_mod, LYD_XML, NULL, NULL, (*type)->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003407 if (err) {
3408 ly_err_print(err);
3409 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3410 "Invalid type's default value \"%s\" which does not fit the type (%s).", dflt, err->msg);
3411 ly_err_free(err);
3412 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02003413 if (ret == LY_EINCOMPLETE) {
3414 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02003415 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, NULL, (*type)->dflt, (*type)->dflt_mod), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02003416
3417 /* but in general result is so far ok */
3418 ret = LY_SUCCESS;
3419 }
Radek Krejcia1911222019-07-22 17:24:50 +02003420 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci01342af2019-01-03 15:18:08 +01003421 }
Radek Krejci19a96102018-11-15 13:38:09 +01003422
Radek Krejci0935f412019-08-20 16:15:18 +02003423 COMPILE_EXTS_GOTO(ctx, type_p->exts, (*type)->exts, (*type), LYEXT_PAR_TYPE, ret, cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01003424
3425cleanup:
3426 ly_set_erase(&tpdf_chain, free);
3427 return ret;
3428}
3429
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003430/**
3431 * @brief Compile status information of the given node.
3432 *
3433 * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes
3434 * has the status correctly set during the compilation.
3435 *
3436 * @param[in] ctx Compile context
3437 * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved.
3438 * If the status was set explicitly on the node, it is already set in the flags value and we just check
3439 * the compatibility with the parent's status value.
3440 * @param[in] parent_flags Flags of the parent node to check/inherit the status value.
3441 * @return LY_ERR value.
3442 */
3443static LY_ERR
3444lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags)
3445{
3446 /* status - it is not inherited by specification, but it does not make sense to have
3447 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3448 if (!((*node_flags) & LYS_STATUS_MASK)) {
3449 if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) {
3450 if ((parent_flags & 0x3) != 0x3) {
3451 /* do not print the warning when inheriting status from uses - the uses_status value has a special
3452 * combination of bits (0x3) which marks the uses_status value */
3453 LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.",
3454 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3455 }
3456 (*node_flags) |= parent_flags & LYS_STATUS_MASK;
3457 } else {
3458 (*node_flags) |= LYS_STATUS_CURR;
3459 }
3460 } else if (parent_flags & LYS_STATUS_MASK) {
3461 /* check status compatibility with the parent */
3462 if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) {
3463 if ((*node_flags) & LYS_STATUS_CURR) {
3464 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3465 "A \"current\" status is in conflict with the parent's \"%s\" status.",
3466 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3467 } else { /* LYS_STATUS_DEPRC */
3468 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3469 "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
3470 }
3471 return LY_EVALID;
3472 }
3473 }
3474 return LY_SUCCESS;
3475}
3476
Radek Krejci8cce8532019-03-05 11:27:45 +01003477/**
3478 * @brief Check uniqness of the node/action/notification name.
3479 *
3480 * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema
3481 * structures, but they share the namespace so we need to check their name collisions.
3482 *
3483 * @param[in] ctx Compile context.
3484 * @param[in] children List (linked list) of data nodes to go through.
3485 * @param[in] actions List (sized array) of actions or RPCs to go through.
3486 * @param[in] notifs List (sized array) of Notifications to go through.
3487 * @param[in] name Name of the item to find in the given lists.
3488 * @param[in] exclude Pointer to an object to exclude from the name checking - for the case that the object
3489 * with the @p name being checked is already inserted into one of the list so we need to skip it when searching for duplicity.
3490 * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise.
3491 */
3492static LY_ERR
3493lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *children,
3494 const struct lysc_action *actions, const struct lysc_notif *notifs,
3495 const char *name, void *exclude)
3496{
3497 const struct lysc_node *iter;
3498 unsigned int u;
3499
3500 LY_LIST_FOR(children, iter) {
3501 if (iter != exclude && iter->module == ctx->mod && !strcmp(name, iter->name)) {
3502 goto error;
3503 }
3504 }
3505 LY_ARRAY_FOR(actions, u) {
3506 if (&actions[u] != exclude && actions[u].module == ctx->mod && !strcmp(name, actions[u].name)) {
3507 goto error;
3508 }
3509 }
3510 LY_ARRAY_FOR(notifs, u) {
3511 if (&notifs[u] != exclude && notifs[u].module == ctx->mod && !strcmp(name, notifs[u].name)) {
3512 goto error;
3513 }
3514 }
3515 return LY_SUCCESS;
3516error:
3517 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/Notification");
3518 return LY_EEXIST;
3519}
3520
Radek Krejciec4da802019-05-02 13:02:41 +02003521static 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 +01003522
Radek Krejcia3045382018-11-22 14:30:31 +01003523/**
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003524 * @brief Compile parsed RPC/action schema node information.
3525 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003526 * @param[in] action_p Parsed RPC/action schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003527 * @param[in] parent Parent node of the action, NULL in case of RPC (top-level action)
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003528 * @param[in,out] action Prepared (empty) compiled action structure to fill.
3529 * @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).
3530 * Zero means no uses, non-zero value with no status bit set mean the default status.
3531 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3532 */
3533static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003534lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003535 struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status)
3536{
3537 LY_ERR ret = LY_SUCCESS;
3538 struct lysp_node *child_p;
3539 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02003540 int opt_prev = ctx->options;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003541
Radek Krejci327de162019-06-14 12:52:07 +02003542 lysc_update_path(ctx, parent, action_p->name);
3543
Radek Krejci8cce8532019-03-05 11:27:45 +01003544 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3545 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3546 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3547 action_p->name, action)) {
3548 return LY_EVALID;
3549 }
3550
Radek Krejciec4da802019-05-02 13:02:41 +02003551 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejci05b774b2019-02-25 13:26:18 +01003552 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003553 "Action \"%s\" is placed inside %s.", action_p->name,
Radek Krejciec4da802019-05-02 13:02:41 +02003554 ctx->options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "Notification");
Radek Krejci05b774b2019-02-25 13:26:18 +01003555 return LY_EVALID;
3556 }
3557
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003558 action->nodetype = LYS_ACTION;
3559 action->module = ctx->mod;
3560 action->parent = parent;
Radek Krejciec4da802019-05-02 13:02:41 +02003561 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003562 action->sp = action_p;
3563 }
3564 action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK;
3565
3566 /* status - it is not inherited by specification, but it does not make sense to have
3567 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3568 LY_CHECK_RET(lys_compile_status(ctx, &action->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
3569
3570 DUP_STRING(ctx->ctx, action_p->name, action->name);
3571 DUP_STRING(ctx->ctx, action_p->dsc, action->dsc);
3572 DUP_STRING(ctx->ctx, action_p->ref, action->ref);
Radek Krejciec4da802019-05-02 13:02:41 +02003573 COMPILE_ARRAY_GOTO(ctx, action_p->iffeatures, action->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003574 COMPILE_EXTS_GOTO(ctx, action_p->exts, action->exts, action, LYEXT_PAR_NODE, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003575
3576 /* input */
Radek Krejci327de162019-06-14 12:52:07 +02003577 lysc_update_path(ctx, (struct lysc_node*)action, "input");
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003578 COMPILE_ARRAY_GOTO(ctx, action_p->input.musts, action->input.musts, u, lys_compile_must, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003579 COMPILE_EXTS_GOTO(ctx, action_p->input.exts, action->input_exts, &action->input, LYEXT_PAR_INPUT, ret, cleanup);
Radek Krejciec4da802019-05-02 13:02:41 +02003580 ctx->options |= LYSC_OPT_RPC_INPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003581 LY_LIST_FOR(action_p->input.data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003582 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003583 }
Radek Krejci327de162019-06-14 12:52:07 +02003584 lysc_update_path(ctx, NULL, NULL);
Radek Krejciec4da802019-05-02 13:02:41 +02003585 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003586
3587 /* output */
Radek Krejci327de162019-06-14 12:52:07 +02003588 lysc_update_path(ctx, (struct lysc_node*)action, "output");
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003589 COMPILE_ARRAY_GOTO(ctx, action_p->output.musts, action->output.musts, u, lys_compile_must, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003590 COMPILE_EXTS_GOTO(ctx, action_p->output.exts, action->output_exts, &action->output, LYEXT_PAR_OUTPUT, ret, cleanup);
Radek Krejciec4da802019-05-02 13:02:41 +02003591 ctx->options |= LYSC_OPT_RPC_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003592 LY_LIST_FOR(action_p->output.data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003593 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003594 }
Radek Krejci327de162019-06-14 12:52:07 +02003595 lysc_update_path(ctx, NULL, NULL);
Radek Krejci327de162019-06-14 12:52:07 +02003596 lysc_update_path(ctx, NULL, NULL);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003597
3598 if ((action_p->input.musts || action_p->output.musts) && !(ctx->options & LYSC_OPT_GROUPING)) {
3599 /* do not check "must" semantics in a grouping */
3600 ly_set_add(&ctx->unres, action, 0);
3601 }
3602
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003603cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003604 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003605 return ret;
3606}
3607
3608/**
Radek Krejci43981a32019-04-12 09:44:11 +02003609 * @brief Compile parsed Notification schema node information.
Radek Krejcifc11bd72019-04-11 16:00:05 +02003610 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003611 * @param[in] notif_p Parsed Notification schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003612 * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification
3613 * @param[in,out] notif Prepared (empty) compiled notification structure to fill.
3614 * @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 +02003615 * Zero means no uses, non-zero value with no status bit set mean the default status.
3616 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3617 */
3618static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003619lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003620 struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status)
3621{
3622 LY_ERR ret = LY_SUCCESS;
3623 struct lysp_node *child_p;
3624 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02003625 int opt_prev = ctx->options;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003626
Radek Krejci327de162019-06-14 12:52:07 +02003627 lysc_update_path(ctx, parent, notif_p->name);
3628
Radek Krejcifc11bd72019-04-11 16:00:05 +02003629 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3630 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3631 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3632 notif_p->name, notif)) {
3633 return LY_EVALID;
3634 }
3635
Radek Krejciec4da802019-05-02 13:02:41 +02003636 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003637 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3638 "Notification \"%s\" is placed inside %s.", notif_p->name,
Radek Krejciec4da802019-05-02 13:02:41 +02003639 ctx->options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another Notification");
Radek Krejcifc11bd72019-04-11 16:00:05 +02003640 return LY_EVALID;
3641 }
3642
3643 notif->nodetype = LYS_NOTIF;
3644 notif->module = ctx->mod;
3645 notif->parent = parent;
Radek Krejciec4da802019-05-02 13:02:41 +02003646 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003647 notif->sp = notif_p;
3648 }
3649 notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK;
3650
3651 /* status - it is not inherited by specification, but it does not make sense to have
3652 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3653 LY_CHECK_RET(lys_compile_status(ctx, &notif->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
3654
3655 DUP_STRING(ctx->ctx, notif_p->name, notif->name);
3656 DUP_STRING(ctx->ctx, notif_p->dsc, notif->dsc);
3657 DUP_STRING(ctx->ctx, notif_p->ref, notif->ref);
Radek Krejciec4da802019-05-02 13:02:41 +02003658 COMPILE_ARRAY_GOTO(ctx, notif_p->iffeatures, notif->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003659 COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, u, lys_compile_must, ret, cleanup);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003660 if (notif_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3661 /* do not check "must" semantics in a grouping */
3662 ly_set_add(&ctx->unres, notif, 0);
3663 }
Radek Krejci0935f412019-08-20 16:15:18 +02003664 COMPILE_EXTS_GOTO(ctx, notif_p->exts, notif->exts, notif, LYEXT_PAR_NODE, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003665
Radek Krejciec4da802019-05-02 13:02:41 +02003666 ctx->options |= LYSC_OPT_NOTIFICATION;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003667 LY_LIST_FOR(notif_p->data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003668 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)notif, uses_status));
Radek Krejcifc11bd72019-04-11 16:00:05 +02003669 }
3670
Radek Krejci327de162019-06-14 12:52:07 +02003671 lysc_update_path(ctx, NULL, NULL);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003672cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003673 ctx->options = opt_prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003674 return ret;
3675}
3676
3677/**
Radek Krejcia3045382018-11-22 14:30:31 +01003678 * @brief Compile parsed container node information.
3679 * @param[in] ctx Compile context
3680 * @param[in] node_p Parsed container node.
Radek Krejcia3045382018-11-22 14:30:31 +01003681 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3682 * is enriched with the container-specific information.
3683 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3684 */
Radek Krejci19a96102018-11-15 13:38:09 +01003685static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003686lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003687{
3688 struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p;
3689 struct lysc_node_container *cont = (struct lysc_node_container*)node;
3690 struct lysp_node *child_p;
3691 unsigned int u;
3692 LY_ERR ret = LY_SUCCESS;
3693
Radek Krejcife909632019-02-12 15:34:42 +01003694 if (cont_p->presence) {
3695 cont->flags |= LYS_PRESENCE;
3696 }
3697
Radek Krejci19a96102018-11-15 13:38:09 +01003698 LY_LIST_FOR(cont_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003699 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci19a96102018-11-15 13:38:09 +01003700 }
3701
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003702 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003703 if (cont_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3704 /* do not check "must" semantics in a grouping */
3705 ly_set_add(&ctx->unres, cont, 0);
3706 }
Radek Krejciec4da802019-05-02 13:02:41 +02003707 COMPILE_ARRAY1_GOTO(ctx, cont_p->actions, cont->actions, node, u, lys_compile_action, 0, ret, done);
3708 COMPILE_ARRAY1_GOTO(ctx, cont_p->notifs, cont->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01003709
3710done:
3711 return ret;
3712}
3713
Radek Krejci33f72892019-02-21 10:36:58 +01003714/*
3715 * @brief Compile type in leaf/leaf-list node and do all the necessary checks.
3716 * @param[in] ctx Compile context.
3717 * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types.
3718 * @param[in] type_p Parsed type to compile.
Radek Krejci33f72892019-02-21 10:36:58 +01003719 * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information.
3720 * @return LY_ERR value.
3721 */
3722static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003723lys_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 +01003724{
Radek Krejcia1911222019-07-22 17:24:50 +02003725 unsigned int u;
Radek Krejci33f72892019-02-21 10:36:58 +01003726 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)leaf;
3727
Radek Krejciec4da802019-05-02 13:02:41 +02003728 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 +01003729 leaf->units ? NULL : &leaf->units));
3730 if (leaf->nodetype == LYS_LEAFLIST) {
3731 if (llist->type->dflt && !llist->dflts && !llist->min) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003732 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts_mods, 1, LY_EMEM);
3733 llist->dflts_mods[0] = llist->type->dflt_mod;
Radek Krejci33f72892019-02-21 10:36:58 +01003734 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, 1, LY_EMEM);
Radek Krejcia1911222019-07-22 17:24:50 +02003735 llist->dflts[0] = calloc(1, sizeof *llist->dflts[0]);
3736 llist->dflts[0]->realtype = llist->type->dflt->realtype;
3737 llist->dflts[0]->realtype->plugin->duplicate(ctx->ctx, llist->type->dflt, llist->dflts[0]);
3738 llist->dflts[0]->realtype->refcount++;
Radek Krejci33f72892019-02-21 10:36:58 +01003739 LY_ARRAY_INCREMENT(llist->dflts);
3740 }
3741 } else {
3742 if (leaf->type->dflt && !leaf->dflt && !(leaf->flags & LYS_MAND_TRUE)) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003743 leaf->dflt_mod = leaf->type->dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02003744 leaf->dflt = calloc(1, sizeof *leaf->dflt);
3745 leaf->dflt->realtype = leaf->type->dflt->realtype;
3746 leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt);
3747 leaf->dflt->realtype->refcount++;
Radek Krejci33f72892019-02-21 10:36:58 +01003748 }
3749 }
3750 if (leaf->type->basetype == LY_TYPE_LEAFREF) {
3751 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
3752 ly_set_add(&ctx->unres, leaf, 0);
3753 } else if (leaf->type->basetype == LY_TYPE_UNION) {
3754 LY_ARRAY_FOR(((struct lysc_type_union*)leaf->type)->types, u) {
3755 if (((struct lysc_type_union*)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
3756 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
3757 ly_set_add(&ctx->unres, leaf, 0);
3758 }
3759 }
3760 } else if (leaf->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci33f72892019-02-21 10:36:58 +01003761 if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) {
3762 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3763 "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
3764 return LY_EVALID;
3765 }
3766 }
3767
Radek Krejci33f72892019-02-21 10:36:58 +01003768 return LY_SUCCESS;
3769}
3770
Radek Krejcia3045382018-11-22 14:30:31 +01003771/**
3772 * @brief Compile parsed leaf node information.
3773 * @param[in] ctx Compile context
3774 * @param[in] node_p Parsed leaf node.
Radek Krejcia3045382018-11-22 14:30:31 +01003775 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3776 * is enriched with the leaf-specific information.
3777 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3778 */
Radek Krejci19a96102018-11-15 13:38:09 +01003779static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003780lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003781{
3782 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p;
3783 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
3784 unsigned int u;
3785 LY_ERR ret = LY_SUCCESS;
3786
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003787 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003788 if (leaf_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3789 /* do not check "must" semantics in a grouping */
3790 ly_set_add(&ctx->unres, leaf, 0);
3791 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003792 if (leaf_p->units) {
3793 leaf->units = lydict_insert(ctx->ctx, leaf_p->units, 0);
3794 leaf->flags |= LYS_SET_UNITS;
3795 }
Radek Krejcia1911222019-07-22 17:24:50 +02003796
3797 /* the dflt member is just filled to avoid getting the default value from the type */
3798 leaf->dflt = (void*)leaf_p->dflt;
3799 ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, leaf);
Radek Krejci812a5bf2019-09-09 09:18:05 +02003800 if (ret) {
3801 leaf->dflt = NULL;
3802 return ret;
3803 }
Radek Krejcia1911222019-07-22 17:24:50 +02003804
Radek Krejciccd20f12019-02-15 14:12:27 +01003805 if (leaf_p->dflt) {
Radek Krejcia1911222019-07-22 17:24:50 +02003806 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003807 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02003808 leaf->dflt = calloc(1, sizeof *leaf->dflt);
3809 leaf->dflt->realtype = leaf->type;
3810 ret = leaf->type->plugin->store(ctx->ctx, leaf->type, leaf_p->dflt, strlen(leaf_p->dflt),
3811 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02003812 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, node, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003813 leaf->dflt->realtype->refcount++;
3814 if (err) {
3815 ly_err_print(err);
3816 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3817 "Invalid leaf's default value \"%s\" which does not fit the type (%s).", leaf_p->dflt, err->msg);
3818 ly_err_free(err);
3819 }
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003820 if (ret == LY_EINCOMPLETE) {
Radek Krejci1c0c3442019-07-23 16:08:47 +02003821 /* postpone default compilation when the tree is complete */
Radek Krejcib5bc93e2019-09-09 09:11:23 +02003822 LY_CHECK_RET(lysc_incomplete_dflts_add(ctx, node, leaf->dflt, leaf->dflt_mod));
Radek Krejci1c0c3442019-07-23 16:08:47 +02003823
3824 /* but in general result is so far ok */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003825 ret = LY_SUCCESS;
3826 }
Radek Krejcib5bc93e2019-09-09 09:11:23 +02003827 LY_CHECK_RET(ret);
Radek Krejci76b3e962018-12-14 17:01:25 +01003828 leaf->flags |= LYS_SET_DFLT;
3829 }
Radek Krejci43699232018-11-23 14:59:46 +01003830
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01003831
Radek Krejci19a96102018-11-15 13:38:09 +01003832done:
3833 return ret;
3834}
3835
Radek Krejcia3045382018-11-22 14:30:31 +01003836/**
Radek Krejci0e5d8382018-11-28 16:37:53 +01003837 * @brief Compile parsed leaf-list node information.
3838 * @param[in] ctx Compile context
3839 * @param[in] node_p Parsed leaf-list node.
Radek Krejci0e5d8382018-11-28 16:37:53 +01003840 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3841 * is enriched with the leaf-list-specific information.
3842 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3843 */
3844static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003845lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci0e5d8382018-11-28 16:37:53 +01003846{
3847 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p;
3848 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
Radek Krejcia1911222019-07-22 17:24:50 +02003849 unsigned int u, v;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003850 LY_ERR ret = LY_SUCCESS;
3851
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003852 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003853 if (llist_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3854 /* do not check "must" semantics in a grouping */
3855 ly_set_add(&ctx->unres, llist, 0);
3856 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003857 if (llist_p->units) {
3858 llist->units = lydict_insert(ctx->ctx, llist_p->units, 0);
3859 llist->flags |= LYS_SET_UNITS;
3860 }
Radek Krejci0e5d8382018-11-28 16:37:53 +01003861
Radek Krejcia1911222019-07-22 17:24:50 +02003862 /* the dflts member is just filled to avoid getting the default value from the type */
3863 llist->dflts = (void*)llist_p->dflts;
3864 ret = lys_compile_node_type(ctx, node_p, &llist_p->type, (struct lysc_node_leaf*)llist);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003865 if (llist_p->dflts) {
Radek Krejcia1911222019-07-22 17:24:50 +02003866 llist->dflts = NULL; /* reset the temporary llist_p->dflts */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003867 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_SIZE(llist_p->dflts), ret, done);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003868 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(llist_p->dflts), ret, done);
3869 LY_ARRAY_FOR(llist_p->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +02003870 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003871 LY_ARRAY_INCREMENT(llist->dflts_mods);
3872 llist->dflts_mods[u] = ctx->mod_def;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003873 LY_ARRAY_INCREMENT(llist->dflts);
Radek Krejcia1911222019-07-22 17:24:50 +02003874 llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]);
3875 llist->dflts[u]->realtype = llist->type;
3876 ret = llist->type->plugin->store(ctx->ctx, llist->type, llist_p->dflts[u], strlen(llist_p->dflts[u]),
3877 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02003878 lys_resolve_prefix, (void*)llist->dflts_mods[u], LYD_XML, node, NULL, llist->dflts[u], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003879 llist->dflts[u]->realtype->refcount++;
3880 if (err) {
3881 ly_err_print(err);
3882 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3883 "Invalid leaf-lists's default value \"%s\" which does not fit the type (%s).", llist_p->dflts[u], err->msg);
3884 ly_err_free(err);
3885 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02003886 if (ret == LY_EINCOMPLETE) {
3887 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02003888 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, llist->dflts[u], llist->dflts_mods[u]), done);
Radek Krejci1c0c3442019-07-23 16:08:47 +02003889
3890 /* but in general result is so far ok */
3891 ret = LY_SUCCESS;
3892 }
Radek Krejcia1911222019-07-22 17:24:50 +02003893 LY_CHECK_GOTO(ret, done);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003894 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003895 llist->flags |= LYS_SET_DFLT;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003896 }
Radek Krejcia1911222019-07-22 17:24:50 +02003897 if ((llist->flags & LYS_CONFIG_W) && llist->dflts && LY_ARRAY_SIZE(llist->dflts)) {
3898 /* configuration data values must be unique - so check the default values */
3899 LY_ARRAY_FOR(llist->dflts, u) {
3900 for (v = u + 1; v < LY_ARRAY_SIZE(llist->dflts); ++v) {
3901 if (!llist->type->plugin->compare(llist->dflts[u], llist->dflts[v])) {
3902 int dynamic = 0;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003903 const char *val = llist->type->plugin->print(llist->dflts[v], LYD_XML, lys_get_prefix, llist->dflts_mods[v], &dynamic);
Radek Krejcia1911222019-07-22 17:24:50 +02003904 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3905 "Configuration leaf-list has multiple defaults of the same value \"%s\".", val);
3906 if (dynamic) {
3907 free((char*)val);
3908 }
3909 return LY_EVALID;
3910 }
3911 }
3912 }
3913 }
3914
3915 /* TODO validate default value according to the type, possibly postpone the check when the leafref target is known */
Radek Krejci0e5d8382018-11-28 16:37:53 +01003916
3917 llist->min = llist_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003918 if (llist->min) {
3919 llist->flags |= LYS_MAND_TRUE;
3920 }
Radek Krejcib7408632018-11-28 17:12:11 +01003921 llist->max = llist_p->max ? llist_p->max : (uint32_t)-1;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003922
Radek Krejci0e5d8382018-11-28 16:37:53 +01003923done:
3924 return ret;
3925}
3926
3927/**
Radek Krejci7af64242019-02-18 13:07:53 +01003928 * @brief Compile information about list's uniques.
3929 * @param[in] ctx Compile context.
3930 * @param[in] context_module Module where the prefixes are going to be resolved.
3931 * @param[in] uniques Sized array list of unique statements.
3932 * @param[in] list Compiled list where the uniques are supposed to be resolved and stored.
3933 * @return LY_ERR value.
3934 */
3935static LY_ERR
3936lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list)
3937{
3938 LY_ERR ret = LY_SUCCESS;
3939 struct lysc_node_leaf **key, ***unique;
3940 const char *keystr, *delim;
3941 size_t len;
3942 unsigned int v;
3943 int config;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003944 uint16_t flags;
Radek Krejci7af64242019-02-18 13:07:53 +01003945
3946 for (v = 0; v < LY_ARRAY_SIZE(uniques); ++v) {
3947 config = -1;
3948 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
3949 keystr = uniques[v];
3950 while (keystr) {
3951 delim = strpbrk(keystr, " \t\n");
3952 if (delim) {
3953 len = delim - keystr;
3954 while (isspace(*delim)) {
3955 ++delim;
3956 }
3957 } else {
3958 len = strlen(keystr);
3959 }
3960
3961 /* unique node must be present */
3962 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003963 ret = lys_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node*)list, context_module, LYS_LEAF, 0,
3964 (const struct lysc_node**)key, &flags);
Radek Krejci7af64242019-02-18 13:07:53 +01003965 if (ret != LY_SUCCESS) {
3966 if (ret == LY_EDENIED) {
3967 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003968 "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.",
Radek Krejci7af64242019-02-18 13:07:53 +01003969 len, keystr, lys_nodetype2str((*key)->nodetype));
3970 }
3971 return LY_EVALID;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003972 } else if (flags) {
3973 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3974 "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.",
3975 len, keystr, flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action");
3976 return LY_EVALID;
Radek Krejci7af64242019-02-18 13:07:53 +01003977 }
3978
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003979
Radek Krejci7af64242019-02-18 13:07:53 +01003980 /* all referenced leafs must be of the same config type */
3981 if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) {
3982 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3983 "Unique statement \"%s\" refers to leafs with different config type.", uniques[v]);
3984 return LY_EVALID;
3985 } else if ((*key)->flags & LYS_CONFIG_W) {
3986 config = 1;
3987 } else { /* LYS_CONFIG_R */
3988 config = 0;
3989 }
3990
3991 /* check status */
3992 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3993 (*key)->flags, (*key)->module, (*key)->name));
3994
3995 /* mark leaf as unique */
3996 (*key)->flags |= LYS_UNIQUE;
3997
3998 /* next unique value in line */
3999 keystr = delim;
4000 }
4001 /* next unique definition */
4002 }
4003
4004 return LY_SUCCESS;
4005}
4006
4007/**
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004008 * @brief Compile parsed list node information.
4009 * @param[in] ctx Compile context
4010 * @param[in] node_p Parsed list node.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004011 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
4012 * is enriched with the list-specific information.
4013 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4014 */
4015static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004016lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004017{
4018 struct lysp_node_list *list_p = (struct lysp_node_list*)node_p;
4019 struct lysc_node_list *list = (struct lysc_node_list*)node;
4020 struct lysp_node *child_p;
Radek Krejci0fe9b512019-07-26 17:51:05 +02004021 struct lysc_node_leaf *key, *prev_key = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004022 size_t len;
Radek Krejci7af64242019-02-18 13:07:53 +01004023 unsigned int u;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004024 const char *keystr, *delim;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004025 LY_ERR ret = LY_SUCCESS;
4026
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004027 list->min = list_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01004028 if (list->min) {
4029 list->flags |= LYS_MAND_TRUE;
4030 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004031 list->max = list_p->max ? list_p->max : (uint32_t)-1;
4032
4033 LY_LIST_FOR(list_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02004034 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004035 }
4036
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004037 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01004038 if (list_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
4039 /* do not check "must" semantics in a grouping */
4040 ly_set_add(&ctx->unres, list, 0);
4041 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004042
4043 /* keys */
4044 if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) {
4045 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
4046 return LY_EVALID;
4047 }
4048
4049 /* find all the keys (must be direct children) */
4050 keystr = list_p->key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02004051 if (!keystr) {
4052 /* keyless list */
4053 list->flags |= LYS_KEYLESS;
4054 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004055 while (keystr) {
4056 delim = strpbrk(keystr, " \t\n");
4057 if (delim) {
4058 len = delim - keystr;
4059 while (isspace(*delim)) {
4060 ++delim;
4061 }
4062 } else {
4063 len = strlen(keystr);
4064 }
4065
4066 /* key node must be present */
Radek Krejci0fe9b512019-07-26 17:51:05 +02004067 key = (struct lysc_node_leaf*)lys_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE | LYS_GETNEXT_NOSTATECHECK);
4068 if (!(key)) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004069 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4070 "The list's key \"%.*s\" not found.", len, keystr);
4071 return LY_EVALID;
4072 }
4073 /* keys must be unique */
Radek Krejci0fe9b512019-07-26 17:51:05 +02004074 if (key->flags & LYS_KEY) {
4075 /* the node was already marked as a key */
4076 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4077 "Duplicated key identifier \"%.*s\".", len, keystr);
4078 return LY_EVALID;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004079 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02004080
4081 lysc_update_path(ctx, (struct lysc_node*)list, key->name);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004082 /* key must have the same config flag as the list itself */
Radek Krejci0fe9b512019-07-26 17:51:05 +02004083 if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004084 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf.");
4085 return LY_EVALID;
4086 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004087 if (ctx->mod_def->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004088 /* YANG 1.0 denies key to be of empty type */
Radek Krejci0fe9b512019-07-26 17:51:05 +02004089 if (key->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004090 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004091 "List's key cannot be of \"empty\" type until it is in YANG 1.1 module.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004092 return LY_EVALID;
4093 }
4094 } else {
4095 /* when and if-feature are illegal on list keys */
Radek Krejci0fe9b512019-07-26 17:51:05 +02004096 if (key->when) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004097 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004098 "List's key must not have any \"when\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004099 return LY_EVALID;
4100 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02004101 if (key->iffeatures) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004102 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004103 "List's key must not have any \"if-feature\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004104 return LY_EVALID;
4105 }
4106 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004107
4108 /* check status */
4109 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
Radek Krejci0fe9b512019-07-26 17:51:05 +02004110 key->flags, key->module, key->name));
Radek Krejci76b3e962018-12-14 17:01:25 +01004111
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004112 /* ignore default values of the key */
Radek Krejci0fe9b512019-07-26 17:51:05 +02004113 if (key->dflt) {
4114 key->dflt->realtype->plugin->free(ctx->ctx, key->dflt);
4115 lysc_type_free(ctx->ctx, key->dflt->realtype);
4116 free(key->dflt);
4117 key->dflt = NULL;
4118 key->dflt_mod = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004119 }
4120 /* mark leaf as key */
Radek Krejci0fe9b512019-07-26 17:51:05 +02004121 key->flags |= LYS_KEY;
4122
4123 /* move it to the correct position */
4124 if ((prev_key && (struct lysc_node*)prev_key != key->prev) || (!prev_key && key->prev->next)) {
4125 /* fix links in closest previous siblings of the key */
4126 if (key->next) {
4127 key->next->prev = key->prev;
4128 } else {
4129 /* last child */
4130 list->child->prev = key->prev;
4131 }
4132 if (key->prev->next) {
4133 key->prev->next = key->next;
4134 }
4135 /* fix links in the key */
4136 if (prev_key) {
4137 key->prev = (struct lysc_node*)prev_key;
4138 key->next = prev_key->next;
4139 } else {
4140 key->prev = list->child->prev;
4141 key->next = list->child;
4142 }
4143 /* fix links in closes future siblings of the key */
4144 if (prev_key) {
4145 if (prev_key->next) {
4146 prev_key->next->prev = (struct lysc_node*)key;
4147 } else {
4148 list->child->prev = (struct lysc_node*)key;
4149 }
4150 prev_key->next = (struct lysc_node*)key;
4151 } else {
4152 list->child->prev = (struct lysc_node*)key;
4153 }
4154 /* fix links in parent */
4155 if (!key->prev->next) {
4156 list->child = (struct lysc_node*)key;
4157 }
4158 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004159
4160 /* next key value */
Radek Krejci0fe9b512019-07-26 17:51:05 +02004161 prev_key = key;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004162 keystr = delim;
Radek Krejci327de162019-06-14 12:52:07 +02004163 lysc_update_path(ctx, NULL, NULL);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004164 }
4165
4166 /* uniques */
4167 if (list_p->uniques) {
Radek Krejci7af64242019-02-18 13:07:53 +01004168 LY_CHECK_RET(lys_compile_node_list_unique(ctx, list->module, list_p->uniques, list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004169 }
4170
Radek Krejciec4da802019-05-02 13:02:41 +02004171 COMPILE_ARRAY1_GOTO(ctx, list_p->actions, list->actions, node, u, lys_compile_action, 0, ret, done);
4172 COMPILE_ARRAY1_GOTO(ctx, list_p->notifs, list->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004173
4174done:
4175 return ret;
4176}
4177
Radek Krejcib56c7502019-02-13 14:19:54 +01004178/**
4179 * @brief Do some checks and set the default choice's case.
4180 *
4181 * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it.
4182 *
4183 * @param[in] ctx Compile context.
4184 * @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,
4185 * not the reference to the imported module.
4186 * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice.
4187 * @return LY_ERR value.
4188 */
Radek Krejci76b3e962018-12-14 17:01:25 +01004189static LY_ERR
4190lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
4191{
4192 struct lysc_node *iter, *node = (struct lysc_node*)ch;
4193 const char *prefix = NULL, *name;
4194 size_t prefix_len = 0;
4195
4196 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
4197 name = strchr(dflt, ':');
4198 if (name) {
4199 prefix = dflt;
4200 prefix_len = name - prefix;
4201 ++name;
4202 } else {
4203 name = dflt;
4204 }
Radek Krejci7f9b6512019-09-18 13:11:09 +02004205 if (prefix && ly_strncmp(node->module->prefix, prefix, prefix_len)) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004206 /* prefixed default case make sense only for the prefix of the schema itself */
4207 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4208 "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").",
4209 prefix_len, prefix);
4210 return LY_EVALID;
4211 }
4212 ch->dflt = (struct lysc_node_case*)lys_child(node, node->module, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE);
4213 if (!ch->dflt) {
4214 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4215 "Default case \"%s\" not found.", dflt);
4216 return LY_EVALID;
4217 }
4218 /* no mandatory nodes directly under the default case */
4219 LY_LIST_FOR(ch->dflt->child, iter) {
Radek Krejcife13da42019-02-15 14:51:01 +01004220 if (iter->parent != (struct lysc_node*)ch->dflt) {
4221 break;
4222 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004223 if (iter->flags & LYS_MAND_TRUE) {
4224 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4225 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt);
4226 return LY_EVALID;
4227 }
4228 }
Radek Krejci01342af2019-01-03 15:18:08 +01004229 ch->dflt->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004230 return LY_SUCCESS;
4231}
4232
Radek Krejciccd20f12019-02-15 14:12:27 +01004233static LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02004234lys_compile_deviation_set_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
Radek Krejciccd20f12019-02-15 14:12:27 +01004235{
4236 struct lys_module *mod;
4237 const char *prefix = NULL, *name;
4238 size_t prefix_len = 0;
4239 struct lysc_node_case *cs;
4240 struct lysc_node *node;
4241
4242 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
4243 name = strchr(dflt, ':');
4244 if (name) {
4245 prefix = dflt;
4246 prefix_len = name - prefix;
4247 ++name;
4248 } else {
4249 name = dflt;
4250 }
4251 /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */
4252 if (prefix) {
4253 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
4254 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004255 "Invalid deviation adding \"default\" property \"%s\" of choice. "
4256 "The prefix does not match any imported module of the deviation module.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01004257 return LY_EVALID;
4258 }
4259 } else {
4260 mod = ctx->mod;
4261 }
4262 /* get the default case */
4263 cs = (struct lysc_node_case*)lys_child((struct lysc_node*)ch, mod, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE);
4264 if (!cs) {
4265 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004266 "Invalid deviation adding \"default\" property \"%s\" of choice - the specified case does not exists.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01004267 return LY_EVALID;
4268 }
4269
4270 /* check that there is no mandatory node */
4271 LY_LIST_FOR(cs->child, node) {
Radek Krejcife13da42019-02-15 14:51:01 +01004272 if (node->parent != (struct lysc_node*)cs) {
4273 break;
4274 }
Radek Krejciccd20f12019-02-15 14:12:27 +01004275 if (node->flags & LYS_MAND_TRUE) {
4276 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004277 "Invalid deviation adding \"default\" property \"%s\" of choice - "
4278 "mandatory node \"%s\" under the default case.", dflt, node->name);
Radek Krejciccd20f12019-02-15 14:12:27 +01004279 return LY_EVALID;
4280 }
4281 }
4282
4283 /* set the default case in choice */
4284 ch->dflt = cs;
4285 cs->flags |= LYS_SET_DFLT;
4286
4287 return LY_SUCCESS;
4288}
4289
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004290/**
Radek Krejci056d0a82018-12-06 16:57:25 +01004291 * @brief Compile parsed choice node information.
4292 * @param[in] ctx Compile context
4293 * @param[in] node_p Parsed choice node.
Radek Krejci056d0a82018-12-06 16:57:25 +01004294 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
Radek Krejci76b3e962018-12-14 17:01:25 +01004295 * is enriched with the choice-specific information.
Radek Krejci056d0a82018-12-06 16:57:25 +01004296 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4297 */
4298static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004299lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01004300{
4301 struct lysp_node_choice *ch_p = (struct lysp_node_choice*)node_p;
4302 struct lysc_node_choice *ch = (struct lysc_node_choice*)node;
4303 struct lysp_node *child_p, *case_child_p;
Radek Krejci056d0a82018-12-06 16:57:25 +01004304 LY_ERR ret = LY_SUCCESS;
4305
Radek Krejci056d0a82018-12-06 16:57:25 +01004306 LY_LIST_FOR(ch_p->child, child_p) {
4307 if (child_p->nodetype == LYS_CASE) {
4308 LY_LIST_FOR(((struct lysp_node_case*)child_p)->child, case_child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02004309 LY_CHECK_RET(lys_compile_node(ctx, case_child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01004310 }
4311 } else {
Radek Krejciec4da802019-05-02 13:02:41 +02004312 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01004313 }
4314 }
4315
4316 /* default branch */
Radek Krejcia9026eb2018-12-12 16:04:47 +01004317 if (ch_p->dflt) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004318 LY_CHECK_RET(lys_compile_node_choice_dflt(ctx, ch_p->dflt, ch));
Radek Krejcia9026eb2018-12-12 16:04:47 +01004319 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004320
Radek Krejci9800fb82018-12-13 14:26:23 +01004321 return ret;
4322}
4323
4324/**
4325 * @brief Compile parsed anydata or anyxml node information.
4326 * @param[in] ctx Compile context
4327 * @param[in] node_p Parsed anydata or anyxml node.
Radek Krejci9800fb82018-12-13 14:26:23 +01004328 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
4329 * is enriched with the any-specific information.
4330 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4331 */
4332static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004333lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9800fb82018-12-13 14:26:23 +01004334{
4335 struct lysp_node_anydata *any_p = (struct lysp_node_anydata*)node_p;
4336 struct lysc_node_anydata *any = (struct lysc_node_anydata*)node;
4337 unsigned int u;
4338 LY_ERR ret = LY_SUCCESS;
4339
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004340 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01004341 if (any_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
4342 /* do not check "must" semantics in a grouping */
4343 ly_set_add(&ctx->unres, any, 0);
4344 }
Radek Krejci9800fb82018-12-13 14:26:23 +01004345
4346 if (any->flags & LYS_CONFIG_W) {
4347 LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended.",
Radek Krejcid6b76452019-09-03 17:03:03 +02004348 ly_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML));
Radek Krejci9800fb82018-12-13 14:26:23 +01004349 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004350done:
4351 return ret;
4352}
4353
Radek Krejcib56c7502019-02-13 14:19:54 +01004354/**
Radek Krejci056d0a82018-12-06 16:57:25 +01004355 * @brief Connect the node into the siblings list and check its name uniqueness.
4356 *
4357 * @param[in] ctx Compile context
4358 * @param[in] parent Parent node holding the children list, in case of node from a choice's case,
4359 * the choice itself is expected instead of a specific case node.
4360 * @param[in] node Schema node to connect into the list.
4361 * @return LY_ERR value - LY_SUCCESS or LY_EEXIST.
4362 */
4363static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004364lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01004365{
4366 struct lysc_node **children;
4367
4368 if (node->nodetype == LYS_CASE) {
4369 children = (struct lysc_node**)&((struct lysc_node_choice*)parent)->cases;
4370 } else {
Radek Krejciec4da802019-05-02 13:02:41 +02004371 children = lysc_node_children_p(parent, ctx->options);
Radek Krejci056d0a82018-12-06 16:57:25 +01004372 }
4373 if (children) {
4374 if (!(*children)) {
4375 /* first child */
4376 *children = node;
4377 } else if (*children != node) {
4378 /* by the condition in previous branch we cover the choice/case children
4379 * - the children list is shared by the choice and the the first case, in addition
4380 * the first child of each case must be referenced from the case node. So the node is
4381 * actually always already inserted in case it is the first children - so here such
4382 * a situation actually corresponds to the first branch */
4383 /* insert at the end of the parent's children list */
4384 (*children)->prev->next = node;
4385 node->prev = (*children)->prev;
4386 (*children)->prev = node;
4387
4388 /* check the name uniqueness */
4389 if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent),
4390 lysc_node_notifs(parent), node->name, node)) {
4391 return LY_EEXIST;
4392 }
4393 }
4394 }
4395 return LY_SUCCESS;
4396}
4397
Radek Krejci95710c92019-02-11 15:49:55 +01004398/**
Radek Krejcib56c7502019-02-13 14:19:54 +01004399 * @brief Prepare the case structure in choice node for the new data node.
4400 *
4401 * 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
4402 * created in the choice when the first child was processed.
4403 *
4404 * @param[in] ctx Compile context.
Radek Krejci95710c92019-02-11 15:49:55 +01004405 * @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,
4406 * it is the LYS_CHOICE node or LYS_AUGMENT node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004407 * @param[in] ch The compiled choice structure where the new case structures are created (if needed).
4408 * @param[in] child The new data node being part of a case (no matter if explicit or implicit).
4409 * @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,
4410 * it is linked from the case structure only in case it is its first child.
Radek Krejci95710c92019-02-11 15:49:55 +01004411 */
Radek Krejci056d0a82018-12-06 16:57:25 +01004412static struct lysc_node_case*
Radek Krejciec4da802019-05-02 13:02:41 +02004413lys_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 +01004414{
4415 struct lysc_node *iter;
Radek Krejci98b1a662019-04-23 10:25:50 +02004416 struct lysc_node_case *cs = NULL;
Radek Krejci00b874b2019-02-12 10:54:50 +01004417 struct lysc_when **when;
Radek Krejci056d0a82018-12-06 16:57:25 +01004418 unsigned int u;
4419 LY_ERR ret;
4420
Radek Krejci95710c92019-02-11 15:49:55 +01004421#define UNIQUE_CHECK(NAME, MOD) \
Radek Krejci056d0a82018-12-06 16:57:25 +01004422 LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \
Radek Krejci95710c92019-02-11 15:49:55 +01004423 if (iter->module == MOD && !strcmp(iter->name, NAME)) { \
Radek Krejci056d0a82018-12-06 16:57:25 +01004424 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \
4425 return NULL; \
4426 } \
4427 }
4428
Radek Krejci95710c92019-02-11 15:49:55 +01004429 if (node_p->nodetype == LYS_CHOICE || node_p->nodetype == LYS_AUGMENT) {
4430 UNIQUE_CHECK(child->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01004431
4432 /* we have to add an implicit case node into the parent choice */
4433 cs = calloc(1, sizeof(struct lysc_node_case));
4434 DUP_STRING(ctx->ctx, child->name, cs->name);
Michal Vasko175012e2019-11-06 15:49:14 +01004435 cs->parent = (struct lysc_node*)ch;
Radek Krejci056d0a82018-12-06 16:57:25 +01004436 cs->flags = ch->flags & LYS_STATUS_MASK;
Radek Krejci95710c92019-02-11 15:49:55 +01004437 } else if (node_p->nodetype == LYS_CASE) {
Radek Krejci056d0a82018-12-06 16:57:25 +01004438 if (ch->cases && (node_p == ch->cases->prev->sp)) {
4439 /* the case is already present since the child is not its first children */
4440 return (struct lysc_node_case*)ch->cases->prev;
4441 }
Radek Krejci95710c92019-02-11 15:49:55 +01004442 UNIQUE_CHECK(node_p->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01004443
4444 /* explicit parent case is not present (this is its first child) */
4445 cs = calloc(1, sizeof(struct lysc_node_case));
4446 DUP_STRING(ctx->ctx, node_p->name, cs->name);
Michal Vasko175012e2019-11-06 15:49:14 +01004447 cs->parent = (struct lysc_node*)ch;
Radek Krejci056d0a82018-12-06 16:57:25 +01004448 cs->flags = LYS_STATUS_MASK & node_p->flags;
4449 cs->sp = node_p;
4450
Radek Krejcib1b59152019-01-07 13:21:56 +01004451 /* 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 +02004452 LY_CHECK_GOTO(lys_compile_status(ctx, &cs->flags, ch->flags), error);
Radek Krejci00b874b2019-02-12 10:54:50 +01004453
4454 if (node_p->when) {
4455 LY_ARRAY_NEW_GOTO(ctx->ctx, cs->when, when, ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01004456 ret = lys_compile_when(ctx, node_p->when, node_p->flags, (struct lysc_node *)cs, when);
Radek Krejci00b874b2019-02-12 10:54:50 +01004457 LY_CHECK_GOTO(ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01004458
4459 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4460 /* do not check "when" semantics in a grouping */
4461 ly_set_add(&ctx->unres, cs, 0);
4462 }
Radek Krejci00b874b2019-02-12 10:54:50 +01004463 }
Radek Krejciec4da802019-05-02 13:02:41 +02004464 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, cs->iffeatures, u, lys_compile_iffeature, ret, error);
Radek Krejci95710c92019-02-11 15:49:55 +01004465 } else {
4466 LOGINT(ctx->ctx);
4467 goto error;
Radek Krejci056d0a82018-12-06 16:57:25 +01004468 }
4469 cs->module = ctx->mod;
4470 cs->prev = (struct lysc_node*)cs;
4471 cs->nodetype = LYS_CASE;
Radek Krejciec4da802019-05-02 13:02:41 +02004472 lys_compile_node_connect(ctx, (struct lysc_node*)ch, (struct lysc_node*)cs);
Radek Krejci056d0a82018-12-06 16:57:25 +01004473 cs->child = child;
4474
4475 return cs;
4476error:
Radek Krejci1b1e9252019-04-24 08:45:50 +02004477 if (cs) {
4478 lysc_node_free(ctx->ctx, (struct lysc_node*)cs);
4479 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004480 return NULL;
4481
4482#undef UNIQUE_CHECK
4483}
4484
Radek Krejcib56c7502019-02-13 14:19:54 +01004485/**
Radek Krejci93dcc392019-02-19 10:43:38 +01004486 * @brief Apply refined or deviated config to the target node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004487 *
4488 * @param[in] ctx Compile context.
Radek Krejci93dcc392019-02-19 10:43:38 +01004489 * @param[in] node Target node where the config is supposed to be changed.
4490 * @param[in] config_flag Node's config flag to be applied to the @p node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004491 * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is
4492 * 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 +01004493 * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes.
4494 * @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 +01004495 * @return LY_ERR value.
4496 */
Radek Krejci76b3e962018-12-14 17:01:25 +01004497static LY_ERR
Radek Krejci93dcc392019-02-19 10:43:38 +01004498lys_compile_change_config(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t config_flag,
Radek Krejci327de162019-06-14 12:52:07 +02004499 int inheriting, int refine_flag)
Radek Krejci76b3e962018-12-14 17:01:25 +01004500{
4501 struct lysc_node *child;
Radek Krejci93dcc392019-02-19 10:43:38 +01004502 uint16_t config = config_flag & LYS_CONFIG_MASK;
Radek Krejci76b3e962018-12-14 17:01:25 +01004503
4504 if (config == (node->flags & LYS_CONFIG_MASK)) {
4505 /* nothing to do */
4506 return LY_SUCCESS;
4507 }
4508
4509 if (!inheriting) {
Radek Krejci93dcc392019-02-19 10:43:38 +01004510 /* explicit change */
Radek Krejci76b3e962018-12-14 17:01:25 +01004511 if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) {
4512 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004513 "Invalid %s of config - configuration node cannot be child of any state data node.",
4514 refine_flag ? "refine" : "deviation");
Radek Krejci76b3e962018-12-14 17:01:25 +01004515 return LY_EVALID;
4516 }
Radek Krejci93dcc392019-02-19 10:43:38 +01004517 node->flags |= LYS_SET_CONFIG;
4518 } else {
4519 if (node->flags & LYS_SET_CONFIG) {
4520 if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) {
4521 /* setting config flags, but have node with explicit config true */
4522 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004523 "Invalid %s of config - configuration node cannot be child of any state data node.",
4524 refine_flag ? "refine" : "deviation");
Radek Krejci93dcc392019-02-19 10:43:38 +01004525 return LY_EVALID;
4526 }
4527 /* do not change config on nodes where the config is explicitely set, this does not apply to
4528 * nodes, which are being changed explicitly (targets of refine or deviation) */
4529 return LY_SUCCESS;
4530 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004531 }
4532 node->flags &= ~LYS_CONFIG_MASK;
4533 node->flags |= config;
4534
4535 /* inherit the change into the children */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004536 LY_LIST_FOR((struct lysc_node*)lysc_node_children(node, 0), child) {
Radek Krejci327de162019-06-14 12:52:07 +02004537 LY_CHECK_RET(lys_compile_change_config(ctx, child, config_flag, 1, refine_flag));
Radek Krejci76b3e962018-12-14 17:01:25 +01004538 }
4539
Radek Krejci76b3e962018-12-14 17:01:25 +01004540 return LY_SUCCESS;
4541}
4542
Radek Krejcib56c7502019-02-13 14:19:54 +01004543/**
4544 * @brief Set LYS_MAND_TRUE flag for the non-presence container parents.
4545 *
4546 * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate
4547 * the flag to such parents from a mandatory children.
4548 *
4549 * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory.
4550 * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag
4551 * (mandatory children was removed).
4552 */
Radek Krejcife909632019-02-12 15:34:42 +01004553void
4554lys_compile_mandatory_parents(struct lysc_node *parent, int add)
4555{
4556 struct lysc_node *iter;
4557
4558 if (add) { /* set flag */
4559 for (; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
4560 parent = parent->parent) {
4561 parent->flags |= LYS_MAND_TRUE;
4562 }
4563 } else { /* unset flag */
4564 for (; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004565 for (iter = (struct lysc_node*)lysc_node_children(parent, 0); iter; iter = iter->next) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004566 if (iter->flags & LYS_MAND_TRUE) {
Radek Krejcife909632019-02-12 15:34:42 +01004567 /* there is another mandatory node */
4568 return;
4569 }
4570 }
4571 /* unset mandatory flag - there is no mandatory children in the non-presence container */
4572 parent->flags &= ~LYS_MAND_TRUE;
4573 }
4574 }
4575}
4576
Radek Krejci056d0a82018-12-06 16:57:25 +01004577/**
Radek Krejci3641f562019-02-13 15:38:40 +01004578 * @brief Internal sorting process for the lys_compile_augment_sort().
4579 * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result.
4580 * @param[in,out] result Sized array to store the sorted list of augments. The array is expected
4581 * to be allocated to hold the complete list, its size is just incremented by adding another item.
4582 */
4583static void
4584lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result)
4585{
4586 unsigned int v;
4587 size_t len;
4588
4589 len = strlen(aug_p->nodeid);
4590 LY_ARRAY_FOR(result, v) {
4591 if (strlen(result[v]->nodeid) <= len) {
4592 continue;
4593 }
4594 if (v < LY_ARRAY_SIZE(result)) {
4595 /* move the rest of array */
4596 memmove(&result[v + 1], &result[v], (LY_ARRAY_SIZE(result) - v) * sizeof *result);
4597 break;
4598 }
4599 }
4600 result[v] = aug_p;
4601 LY_ARRAY_INCREMENT(result);
4602}
4603
4604/**
4605 * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment).
4606 *
4607 * The sorting is based only on the length of the augment's path since it guarantee the correct order
4608 * (it doesn't matter the /a/x is done before /a/b/c from the example above).
4609 *
4610 * @param[in] ctx Compile context.
4611 * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken).
4612 * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's)
4613 * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules.
4614 * @param[out] augments Resulting sorted sized array of pointers to the augments.
4615 * @return LY_ERR value.
4616 */
4617LY_ERR
4618lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments)
4619{
4620 struct lysp_augment **result = NULL;
4621 unsigned int u, v;
4622 size_t count = 0;
4623
4624 assert(augments);
4625
4626 /* get count of the augments in module and all its submodules */
4627 if (aug_p) {
4628 count += LY_ARRAY_SIZE(aug_p);
4629 }
4630 LY_ARRAY_FOR(inc_p, u) {
4631 if (inc_p[u].submodule->augments) {
4632 count += LY_ARRAY_SIZE(inc_p[u].submodule->augments);
4633 }
4634 }
4635
4636 if (!count) {
4637 *augments = NULL;
4638 return LY_SUCCESS;
4639 }
4640 LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM);
4641
4642 /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them
4643 * together, so there can be even /z/y betwwen them. */
4644 LY_ARRAY_FOR(aug_p, u) {
4645 lys_compile_augment_sort_(&aug_p[u], result);
4646 }
4647 LY_ARRAY_FOR(inc_p, u) {
4648 LY_ARRAY_FOR(inc_p[u].submodule->augments, v) {
4649 lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result);
4650 }
4651 }
4652
4653 *augments = result;
4654 return LY_SUCCESS;
4655}
4656
4657/**
4658 * @brief Compile the parsed augment connecting it into its target.
4659 *
4660 * It is expected that all the data referenced in path are present - augments are ordered so that augment B
4661 * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path
4662 * are already implemented and compiled.
4663 *
4664 * @param[in] ctx Compile context.
4665 * @param[in] aug_p Parsed augment to compile.
Radek Krejci3641f562019-02-13 15:38:40 +01004666 * @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
4667 * children in case of the augmenting uses data.
4668 * @return LY_SUCCESS on success.
4669 * @return LY_EVALID on failure.
4670 */
4671LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004672lys_compile_augment(struct lysc_ctx *ctx, struct lysp_augment *aug_p, const struct lysc_node *parent)
Radek Krejci3641f562019-02-13 15:38:40 +01004673{
4674 LY_ERR ret = LY_SUCCESS;
4675 struct lysp_node *node_p, *case_node_p;
4676 struct lysc_node *target; /* target target of the augment */
4677 struct lysc_node *node;
Radek Krejci3641f562019-02-13 15:38:40 +01004678 struct lysc_when **when, *when_shared;
4679 int allow_mandatory = 0;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004680 uint16_t flags = 0;
4681 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02004682 int opt_prev = ctx->options;
Radek Krejci3641f562019-02-13 15:38:40 +01004683
Radek Krejci327de162019-06-14 12:52:07 +02004684 lysc_update_path(ctx, NULL, "{augment}");
4685 lysc_update_path(ctx, NULL, aug_p->nodeid);
4686
Radek Krejci7af64242019-02-18 13:07:53 +01004687 ret = lys_resolve_schema_nodeid(ctx, aug_p->nodeid, 0, parent, parent ? parent->module : ctx->mod_def,
Radek Krejci3641f562019-02-13 15:38:40 +01004688 LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004689 1, (const struct lysc_node**)&target, &flags);
Radek Krejci3641f562019-02-13 15:38:40 +01004690 if (ret != LY_SUCCESS) {
4691 if (ret == LY_EDENIED) {
4692 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4693 "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.",
4694 parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype));
4695 }
4696 return LY_EVALID;
4697 }
4698
4699 /* check for mandatory nodes
4700 * - new cases augmenting some choice can have mandatory nodes
4701 * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement
4702 */
Radek Krejci733988a2019-02-15 15:12:44 +01004703 if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) {
Radek Krejci3641f562019-02-13 15:38:40 +01004704 allow_mandatory = 1;
4705 }
4706
4707 when_shared = NULL;
4708 LY_LIST_FOR(aug_p->child, node_p) {
4709 /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */
4710 if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE)
4711 && !((target->nodetype & (LYS_CONTAINER | LYS_LIST)) && (node_p->nodetype & (LYS_ACTION | LYS_NOTIF)))
Radek Krejci2d56a892019-02-19 09:05:26 +01004712 && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES)
4713 && !(node_p->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_CHOICE | LYS_LEAF | LYS_LIST | LYS_LEAFLIST))) {
Radek Krejci3641f562019-02-13 15:38:40 +01004714 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004715 "Invalid augment of %s node which is not allowed to contain %s node \"%s\".",
4716 lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004717 return LY_EVALID;
4718 }
4719
4720 /* compile the children */
Radek Krejciec4da802019-05-02 13:02:41 +02004721 ctx->options |= flags;
Radek Krejci3641f562019-02-13 15:38:40 +01004722 if (node_p->nodetype != LYS_CASE) {
Radek Krejciec4da802019-05-02 13:02:41 +02004723 LY_CHECK_RET(lys_compile_node(ctx, node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004724 } else {
4725 LY_LIST_FOR(((struct lysp_node_case *)node_p)->child, case_node_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02004726 LY_CHECK_RET(lys_compile_node(ctx, case_node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004727 }
4728 }
Radek Krejciec4da802019-05-02 13:02:41 +02004729 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004730
Radek Krejcife13da42019-02-15 14:51:01 +01004731 /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children,
4732 * here we gets the last created node as last children of our parent */
Radek Krejci3641f562019-02-13 15:38:40 +01004733 if (target->nodetype == LYS_CASE) {
Radek Krejcife13da42019-02-15 14:51:01 +01004734 /* 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 +01004735 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 +01004736 } else if (target->nodetype == LYS_CHOICE) {
4737 /* to pass when statement, we need the last case no matter if it is explicit or implicit case */
4738 node = ((struct lysc_node_choice*)target)->cases->prev;
4739 } else {
4740 /* the compiled node is the last child of the target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004741 node = lysc_node_children(target, flags)->prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004742 }
4743
Radek Krejci733988a2019-02-15 15:12:44 +01004744 if (!allow_mandatory && (node->flags & LYS_CONFIG_W) && (node->flags & LYS_MAND_TRUE)) {
Radek Krejci3641f562019-02-13 15:38:40 +01004745 node->flags &= ~LYS_MAND_TRUE;
4746 lys_compile_mandatory_parents(target, 0);
4747 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004748 "Invalid augment adding mandatory node \"%s\" without making it conditional via when statement.", node->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004749 return LY_EVALID;
4750 }
4751
4752 /* pass augment's when to all the children */
4753 if (aug_p->when) {
4754 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
4755 if (!when_shared) {
Michal Vasko175012e2019-11-06 15:49:14 +01004756 ret = lys_compile_when(ctx, aug_p->when, aug_p->flags, target, when);
Radek Krejci3641f562019-02-13 15:38:40 +01004757 LY_CHECK_GOTO(ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01004758
4759 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4760 /* do not check "when" semantics in a grouping */
Michal Vasko5c4e5892019-11-14 12:31:38 +01004761 ly_set_add(&ctx->unres, node, 0);
Michal Vasko175012e2019-11-06 15:49:14 +01004762 }
4763
Radek Krejci3641f562019-02-13 15:38:40 +01004764 when_shared = *when;
4765 } else {
4766 ++when_shared->refcount;
4767 (*when) = when_shared;
Michal Vasko5c4e5892019-11-14 12:31:38 +01004768
4769 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4770 /* in this case check "when" again for all children because of dummy node check */
4771 ly_set_add(&ctx->unres, node, 0);
4772 }
Radek Krejci3641f562019-02-13 15:38:40 +01004773 }
4774 }
4775 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004776
Radek Krejciec4da802019-05-02 13:02:41 +02004777 ctx->options |= flags;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004778 switch (target->nodetype) {
4779 case LYS_CONTAINER:
Radek Krejci05b774b2019-02-25 13:26:18 +01004780 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_container*)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004781 u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004782 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_container*)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004783 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004784 break;
4785 case LYS_LIST:
Radek Krejci05b774b2019-02-25 13:26:18 +01004786 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_list*)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004787 u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004788 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_list*)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004789 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004790 break;
4791 default:
Radek Krejciec4da802019-05-02 13:02:41 +02004792 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004793 if (aug_p->actions) {
4794 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004795 "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".",
4796 lys_nodetype2str(target->nodetype), aug_p->actions[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004797 return LY_EVALID;
4798 }
4799 if (aug_p->notifs) {
4800 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004801 "Invalid augment of %s node which is not allowed to contain Notification node \"%s\".",
4802 lys_nodetype2str(target->nodetype), aug_p->notifs[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004803 return LY_EVALID;
4804 }
4805 }
Radek Krejci3641f562019-02-13 15:38:40 +01004806
Radek Krejci327de162019-06-14 12:52:07 +02004807 lysc_update_path(ctx, NULL, NULL);
4808 lysc_update_path(ctx, NULL, NULL);
Radek Krejci3641f562019-02-13 15:38:40 +01004809error:
Radek Krejciec4da802019-05-02 13:02:41 +02004810 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004811 return ret;
4812}
4813
4814/**
Radek Krejcif1421c22019-02-19 13:05:20 +01004815 * @brief Apply refined or deviated mandatory flag to the target node.
4816 *
4817 * @param[in] ctx Compile context.
4818 * @param[in] node Target node where the mandatory property is supposed to be changed.
4819 * @param[in] mandatory_flag Node's mandatory flag to be applied to the @p node.
Radek Krejcif1421c22019-02-19 13:05:20 +01004820 * @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 +01004821 * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations,
4822 * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure,
4823 * 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 +01004824 * @return LY_ERR value.
4825 */
4826static LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02004827lys_compile_change_mandatory(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t mandatory_flag, int refine_flag)
Radek Krejcif1421c22019-02-19 13:05:20 +01004828{
4829 if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) {
4830 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004831 "Invalid %s of mandatory - %s cannot hold mandatory statement.",
4832 refine_flag ? "refine" : "deviation", lys_nodetype2str(node->nodetype));
Radek Krejcif1421c22019-02-19 13:05:20 +01004833 return LY_EVALID;
4834 }
4835
4836 if (mandatory_flag & LYS_MAND_TRUE) {
4837 /* check if node has default value */
4838 if (node->nodetype & LYS_LEAF) {
4839 if (node->flags & LYS_SET_DFLT) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004840 if (refine_flag) {
4841 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004842 "Invalid refine of mandatory - leaf already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004843 return LY_EVALID;
4844 }
Radek Krejcia1911222019-07-22 17:24:50 +02004845 } else if (((struct lysc_node_leaf*)node)->dflt) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004846 /* remove the default value taken from the leaf's type */
Radek Krejcia1911222019-07-22 17:24:50 +02004847 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
Radek Krejci474f9b82019-07-24 11:36:37 +02004848
4849 /* update the list of incomplete default values if needed */
4850 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
4851
Radek Krejcia1911222019-07-22 17:24:50 +02004852 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
4853 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
4854 free(leaf->dflt);
4855 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004856 leaf->dflt_mod = NULL;
Radek Krejcif1421c22019-02-19 13:05:20 +01004857 }
4858 } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004859 if (refine_flag) {
4860 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004861 "Invalid refine of mandatory - choice already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004862 return LY_EVALID;
4863 }
Radek Krejcif1421c22019-02-19 13:05:20 +01004864 }
Radek Krejci551b12c2019-02-19 16:11:21 +01004865 if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) {
Radek Krejci327de162019-06-14 12:52:07 +02004866 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid refine of mandatory under the default case.");
Radek Krejcif1421c22019-02-19 13:05:20 +01004867 return LY_EVALID;
4868 }
4869
4870 node->flags &= ~LYS_MAND_FALSE;
4871 node->flags |= LYS_MAND_TRUE;
4872 lys_compile_mandatory_parents(node->parent, 1);
4873 } else {
4874 /* make mandatory false */
4875 node->flags &= ~LYS_MAND_TRUE;
4876 node->flags |= LYS_MAND_FALSE;
4877 lys_compile_mandatory_parents(node->parent, 0);
Radek Krejcia1911222019-07-22 17:24:50 +02004878 if ((node->nodetype & LYS_LEAF) && !((struct lysc_node_leaf*)node)->dflt && ((struct lysc_node_leaf*)node)->type->dflt) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004879 /* get the type's default value if any */
Radek Krejcia1911222019-07-22 17:24:50 +02004880 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004881 leaf->dflt_mod = leaf->type->dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02004882 leaf->dflt = calloc(1, sizeof *leaf->dflt);
4883 leaf->dflt->realtype = leaf->type->dflt->realtype;
4884 leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt);
4885 leaf->dflt->realtype->refcount++;
Radek Krejcif1421c22019-02-19 13:05:20 +01004886 }
4887 }
4888 return LY_SUCCESS;
4889}
4890
4891/**
Radek Krejcie86bf772018-12-14 11:39:53 +01004892 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
4893 * If present, also apply uses's modificators.
4894 *
4895 * @param[in] ctx Compile context
4896 * @param[in] uses_p Parsed uses schema node.
Radek Krejcie86bf772018-12-14 11:39:53 +01004897 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
4898 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
4899 * the compile context.
4900 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4901 */
4902static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004903lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent)
Radek Krejcie86bf772018-12-14 11:39:53 +01004904{
4905 struct lysp_node *node_p;
Radek Krejci01342af2019-01-03 15:18:08 +01004906 struct lysc_node *node, *child;
Radek Krejci12fb9142019-01-08 09:45:30 +01004907 /* context_node_fake allows us to temporarily isolate the nodes inserted from the grouping instead of uses */
Radek Krejci01342af2019-01-03 15:18:08 +01004908 struct lysc_node_container context_node_fake =
4909 {.nodetype = LYS_CONTAINER,
4910 .module = ctx->mod,
4911 .flags = parent ? parent->flags : 0,
4912 .child = NULL, .next = NULL,
Radek Krejcifc11bd72019-04-11 16:00:05 +02004913 .prev = (struct lysc_node*)&context_node_fake,
4914 .actions = NULL, .notifs = NULL};
Radek Krejciec4da802019-05-02 13:02:41 +02004915 struct lysp_grp *grp = NULL;
Radek Krejci76b3e962018-12-14 17:01:25 +01004916 unsigned int u, v, grp_stack_count;
Radek Krejcie86bf772018-12-14 11:39:53 +01004917 int found;
4918 const char *id, *name, *prefix;
4919 size_t prefix_len, name_len;
4920 struct lys_module *mod, *mod_old;
Radek Krejci76b3e962018-12-14 17:01:25 +01004921 struct lysp_refine *rfn;
Radek Krejcia1911222019-07-22 17:24:50 +02004922 LY_ERR ret = LY_EVALID, rc;
Radek Krejcif2271f12019-01-07 16:42:23 +01004923 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004924 uint16_t flags;
Radek Krejcif2271f12019-01-07 16:42:23 +01004925 struct ly_set refined = {0};
Radek Krejci00b874b2019-02-12 10:54:50 +01004926 struct lysc_when **when, *when_shared;
Radek Krejci3641f562019-02-13 15:38:40 +01004927 struct lysp_augment **augments = NULL;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004928 unsigned int actions_index, notifs_index;
4929 struct lysc_notif **notifs = NULL;
4930 struct lysc_action **actions = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01004931
4932 /* search for the grouping definition */
4933 found = 0;
4934 id = uses_p->name;
Radek Krejcib4a4a272019-06-10 12:44:52 +02004935 LY_CHECK_RET(ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID);
Radek Krejcie86bf772018-12-14 11:39:53 +01004936 if (prefix) {
4937 mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len);
4938 if (!mod) {
4939 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004940 "Invalid prefix used for grouping reference.", uses_p->name);
Radek Krejcie86bf772018-12-14 11:39:53 +01004941 return LY_EVALID;
4942 }
4943 } else {
4944 mod = ctx->mod_def;
4945 }
4946 if (mod == ctx->mod_def) {
4947 for (node_p = uses_p->parent; !found && node_p; node_p = node_p->parent) {
Radek Krejciec4da802019-05-02 13:02:41 +02004948 grp = (struct lysp_grp*)lysp_node_groupings(node_p);
Radek Krejcie86bf772018-12-14 11:39:53 +01004949 LY_ARRAY_FOR(grp, u) {
4950 if (!strcmp(grp[u].name, name)) {
4951 grp = &grp[u];
4952 found = 1;
4953 break;
4954 }
4955 }
4956 }
4957 }
4958 if (!found) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004959 /* search in top-level groupings of the main module ... */
Radek Krejcie86bf772018-12-14 11:39:53 +01004960 grp = mod->parsed->groupings;
Radek Krejci76b3e962018-12-14 17:01:25 +01004961 if (grp) {
4962 for (u = 0; !found && u < LY_ARRAY_SIZE(grp); ++u) {
4963 if (!strcmp(grp[u].name, name)) {
4964 grp = &grp[u];
4965 found = 1;
4966 }
4967 }
4968 }
4969 if (!found && mod->parsed->includes) {
4970 /* ... and all the submodules */
4971 for (u = 0; !found && u < LY_ARRAY_SIZE(mod->parsed->includes); ++u) {
4972 grp = mod->parsed->includes[u].submodule->groupings;
4973 if (grp) {
4974 for (v = 0; !found && v < LY_ARRAY_SIZE(grp); ++v) {
4975 if (!strcmp(grp[v].name, name)) {
4976 grp = &grp[v];
4977 found = 1;
4978 }
4979 }
4980 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004981 }
4982 }
4983 }
4984 if (!found) {
4985 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4986 "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
4987 return LY_EVALID;
4988 }
4989
4990 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
4991 grp_stack_count = ctx->groupings.count;
4992 ly_set_add(&ctx->groupings, (void*)grp, 0);
4993 if (grp_stack_count == ctx->groupings.count) {
4994 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
4995 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4996 "Grouping \"%s\" references itself through a uses statement.", grp->name);
4997 return LY_EVALID;
4998 }
Radek Krejcif2de0ed2019-05-02 14:13:18 +02004999 if (!(ctx->options & LYSC_OPT_GROUPING)) {
5000 /* remember that the grouping is instantiated to avoid its standalone validation */
5001 grp->flags |= LYS_USED_GRP;
5002 }
Radek Krejcie86bf772018-12-14 11:39:53 +01005003
5004 /* switch context's mod_def */
5005 mod_old = ctx->mod_def;
5006 ctx->mod_def = mod;
5007
5008 /* check status */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005009 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 +01005010
Radek Krejcifc11bd72019-04-11 16:00:05 +02005011 /* compile data nodes */
Radek Krejcie86bf772018-12-14 11:39:53 +01005012 LY_LIST_FOR(grp->data, node_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01005013 /* 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 +02005014 LY_CHECK_GOTO(lys_compile_node(ctx, node_p, parent, (uses_p->flags & LYS_STATUS_MASK) | 0x3), cleanup);
Radek Krejci00b874b2019-02-12 10:54:50 +01005015
5016 /* some preparation for applying refines */
5017 if (grp->data == node_p) {
5018 /* remember the first child */
Radek Krejci684faf22019-05-27 14:31:16 +02005019 if (parent) {
5020 child = (struct lysc_node*)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK);
5021 } else if (ctx->mod->compiled->data) {
5022 child = ctx->mod->compiled->data;
5023 } else {
5024 child = NULL;
5025 }
5026 context_node_fake.child = child ? child->prev : NULL;
Radek Krejci01342af2019-01-03 15:18:08 +01005027 }
5028 }
Radek Krejci00b874b2019-02-12 10:54:50 +01005029 when_shared = NULL;
Radek Krejci01342af2019-01-03 15:18:08 +01005030 LY_LIST_FOR(context_node_fake.child, child) {
5031 child->parent = (struct lysc_node*)&context_node_fake;
Radek Krejci00b874b2019-02-12 10:54:50 +01005032
Radek Krejcifc11bd72019-04-11 16:00:05 +02005033 /* pass uses's when to all the data children, actions and notifications are ignored */
Radek Krejci00b874b2019-02-12 10:54:50 +01005034 if (uses_p->when) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005035 LY_ARRAY_NEW_GOTO(ctx->ctx, child->when, when, ret, cleanup);
Radek Krejci00b874b2019-02-12 10:54:50 +01005036 if (!when_shared) {
Michal Vasko175012e2019-11-06 15:49:14 +01005037 LY_CHECK_GOTO(lys_compile_when(ctx, uses_p->when, uses_p->flags, parent, when), cleanup);
5038
5039 if (!(ctx->options & LYSC_OPT_GROUPING)) {
5040 /* do not check "when" semantics in a grouping */
5041 ly_set_add(&ctx->unres, child, 0);
5042 }
5043
Radek Krejci00b874b2019-02-12 10:54:50 +01005044 when_shared = *when;
5045 } else {
5046 ++when_shared->refcount;
5047 (*when) = when_shared;
Michal Vasko5c4e5892019-11-14 12:31:38 +01005048
5049 if (!(ctx->options & LYSC_OPT_GROUPING)) {
5050 /* in this case check "when" again for all children because of dummy node check */
5051 ly_set_add(&ctx->unres, child, 0);
5052 }
Radek Krejci00b874b2019-02-12 10:54:50 +01005053 }
5054 }
Radek Krejci01342af2019-01-03 15:18:08 +01005055 }
5056 if (context_node_fake.child) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005057 /* child is the last data node added by grouping */
Radek Krejci01342af2019-01-03 15:18:08 +01005058 child = context_node_fake.child->prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005059 /* fix child link of our fake container to point to the first child of the original list */
Radek Krejciec4da802019-05-02 13:02:41 +02005060 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 +01005061 }
5062
Radek Krejcifc11bd72019-04-11 16:00:05 +02005063 /* compile actions */
5064 actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs;
5065 if (actions) {
5066 actions_index = *actions ? LY_ARRAY_SIZE(*actions) : 0;
Radek Krejciec4da802019-05-02 13:02:41 +02005067 COMPILE_ARRAY1_GOTO(ctx, grp->actions, *actions, parent, u, lys_compile_action, 0, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005068 if (*actions && (uses_p->augments || uses_p->refines)) {
5069 /* but for augment and refine, we need to separate the compiled grouping's actions to avoid modification of others */
5070 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_SIZE(*actions) - actions_index, ret, cleanup);
5071 LY_ARRAY_SIZE(context_node_fake.actions) = LY_ARRAY_SIZE(*actions) - actions_index;
5072 memcpy(context_node_fake.actions, &(*actions)[actions_index], LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions);
5073 }
5074 }
5075
5076 /* compile notifications */
5077 notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs;
5078 if (notifs) {
5079 notifs_index = *notifs ? LY_ARRAY_SIZE(*notifs) : 0;
Radek Krejciec4da802019-05-02 13:02:41 +02005080 COMPILE_ARRAY1_GOTO(ctx, grp->notifs, *notifs, parent, u, lys_compile_notif, 0, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005081 if (*notifs && (uses_p->augments || uses_p->refines)) {
5082 /* but for augment and refine, we need to separate the compiled grouping's notification to avoid modification of others */
5083 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_SIZE(*notifs) - notifs_index, ret, cleanup);
5084 LY_ARRAY_SIZE(context_node_fake.notifs) = LY_ARRAY_SIZE(*notifs) - notifs_index;
5085 memcpy(context_node_fake.notifs, &(*notifs)[notifs_index], LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs);
5086 }
5087 }
5088
5089
Radek Krejci3641f562019-02-13 15:38:40 +01005090 /* sort and apply augments */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005091 LY_CHECK_GOTO(lys_compile_augment_sort(ctx, uses_p->augments, NULL, &augments), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01005092 LY_ARRAY_FOR(augments, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02005093 LY_CHECK_GOTO(lys_compile_augment(ctx, augments[u], (struct lysc_node*)&context_node_fake), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01005094 }
Radek Krejci12fb9142019-01-08 09:45:30 +01005095
Radek Krejcif0089082019-01-07 16:42:01 +01005096 /* reload previous context's mod_def */
5097 ctx->mod_def = mod_old;
Radek Krejci327de162019-06-14 12:52:07 +02005098 lysc_update_path(ctx, NULL, "{refine}");
Radek Krejcif0089082019-01-07 16:42:01 +01005099
Radek Krejci76b3e962018-12-14 17:01:25 +01005100 /* apply refine */
5101 LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) {
Radek Krejci327de162019-06-14 12:52:07 +02005102 lysc_update_path(ctx, NULL, rfn->nodeid);
5103
Radek Krejci7af64242019-02-18 13:07:53 +01005104 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 +01005105 0, 0, (const struct lysc_node**)&node, &flags),
Radek Krejcifc11bd72019-04-11 16:00:05 +02005106 cleanup);
Radek Krejcif2271f12019-01-07 16:42:23 +01005107 ly_set_add(&refined, node, LY_SET_OPT_USEASLIST);
Radek Krejci76b3e962018-12-14 17:01:25 +01005108
5109 /* default value */
5110 if (rfn->dflts) {
Radek Krejci01342af2019-01-03 15:18:08 +01005111 if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_SIZE(rfn->dflts) > 1) {
Radek Krejci76b3e962018-12-14 17:01:25 +01005112 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005113 "Invalid refine of default - %s cannot hold %d default values.",
5114 lys_nodetype2str(node->nodetype), LY_ARRAY_SIZE(rfn->dflts));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005115 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01005116 }
5117 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) {
5118 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005119 "Invalid refine of default - %s cannot hold default value(s).",
5120 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005121 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01005122 }
5123 if (node->nodetype == LYS_LEAF) {
Radek Krejcia1911222019-07-22 17:24:50 +02005124 struct ly_err_item *err = NULL;
5125 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
5126 if (leaf->dflt) {
5127 /* remove the previous default value */
Radek Krejci474f9b82019-07-24 11:36:37 +02005128 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02005129 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
5130 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
5131 } else {
5132 /* prepare a new one */
5133 leaf->dflt = calloc(1, sizeof *leaf->dflt);
5134 leaf->dflt->realtype = leaf->type;
5135 }
5136 /* parse the new one */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005137 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02005138 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, rfn->dflts[0], strlen(rfn->dflts[0]),
5139 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02005140 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, node, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02005141 leaf->dflt->realtype->refcount++;
5142 if (err) {
5143 ly_err_print(err);
5144 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5145 "Invalid refine of default - value \"%s\" does not fit the type (%s).", rfn->dflts[0], err->msg);
5146 ly_err_free(err);
5147 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02005148 if (rc == LY_EINCOMPLETE) {
5149 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02005150 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, leaf->dflt, leaf->dflt_mod), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02005151
5152 /* but in general result is so far ok */
5153 rc = LY_SUCCESS;
5154 }
Radek Krejcia1911222019-07-22 17:24:50 +02005155 LY_CHECK_GOTO(rc, cleanup);
5156 leaf->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01005157 } else if (node->nodetype == LYS_LEAFLIST) {
Radek Krejcia1911222019-07-22 17:24:50 +02005158 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
5159
Radek Krejci0bcdaed2019-01-10 10:21:34 +01005160 if (ctx->mod->version < 2) {
Radek Krejci01342af2019-01-03 15:18:08 +01005161 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5162 "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 +02005163 goto cleanup;
Radek Krejci01342af2019-01-03 15:18:08 +01005164 }
Radek Krejcia1911222019-07-22 17:24:50 +02005165
5166 /* remove previous set of default values */
5167 LY_ARRAY_FOR(llist->dflts, u) {
Radek Krejci474f9b82019-07-24 11:36:37 +02005168 lysc_incomplete_dflts_remove(ctx, llist->dflts[u]);
Radek Krejcia1911222019-07-22 17:24:50 +02005169 llist->dflts[u]->realtype->plugin->free(ctx->ctx, llist->dflts[u]);
5170 lysc_type_free(ctx->ctx, llist->dflts[u]->realtype);
5171 free(llist->dflts[u]);
Radek Krejci76b3e962018-12-14 17:01:25 +01005172 }
Radek Krejcia1911222019-07-22 17:24:50 +02005173 LY_ARRAY_FREE(llist->dflts);
5174 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005175 LY_ARRAY_FREE(llist->dflts_mods);
5176 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02005177
5178 /* create the new set of the default values */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005179 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_SIZE(rfn->dflts), ret, cleanup);
Radek Krejcia1911222019-07-22 17:24:50 +02005180 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(rfn->dflts), ret, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01005181 LY_ARRAY_FOR(rfn->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +02005182 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005183 LY_ARRAY_INCREMENT(llist->dflts_mods);
5184 llist->dflts_mods[u] = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02005185 LY_ARRAY_INCREMENT(llist->dflts);
5186 llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]);
5187 llist->dflts[u]->realtype = llist->type;
Radek Krejci1c0c3442019-07-23 16:08:47 +02005188 rc = llist->type->plugin->store(ctx->ctx, llist->type, rfn->dflts[u], strlen(rfn->dflts[u]),
Radek Krejcia1911222019-07-22 17:24:50 +02005189 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02005190 lys_resolve_prefix, (void*)llist->dflts_mods[u], LYD_XML, node, NULL, llist->dflts[u], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02005191 llist->dflts[u]->realtype->refcount++;
5192 if (err) {
5193 ly_err_print(err);
5194 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5195 "Invalid refine of default in leaf-lists - value \"%s\" does not fit the type (%s).", rfn->dflts[u], err->msg);
5196 ly_err_free(err);
5197 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02005198 if (rc == LY_EINCOMPLETE) {
5199 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02005200 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, llist->dflts[u], llist->dflts_mods[u]), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02005201
5202 /* but in general result is so far ok */
5203 rc = LY_SUCCESS;
5204 }
5205 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01005206 }
Radek Krejcia1911222019-07-22 17:24:50 +02005207 llist->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01005208 } else if (node->nodetype == LYS_CHOICE) {
Radek Krejci01342af2019-01-03 15:18:08 +01005209 if (((struct lysc_node_choice*)node)->dflt) {
5210 /* unset LYS_SET_DFLT from the current default case */
5211 ((struct lysc_node_choice*)node)->dflt->flags &= ~LYS_SET_DFLT;
5212 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02005213 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 +01005214 }
5215 }
5216
Radek Krejci12fb9142019-01-08 09:45:30 +01005217 /* description */
5218 if (rfn->dsc) {
5219 FREE_STRING(ctx->ctx, node->dsc);
5220 node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0);
5221 }
5222
5223 /* reference */
5224 if (rfn->ref) {
5225 FREE_STRING(ctx->ctx, node->ref);
5226 node->ref = lydict_insert(ctx->ctx, rfn->ref, 0);
5227 }
Radek Krejci76b3e962018-12-14 17:01:25 +01005228
5229 /* config */
5230 if (rfn->flags & LYS_CONFIG_MASK) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005231 if (!flags) {
Radek Krejci327de162019-06-14 12:52:07 +02005232 LY_CHECK_GOTO(lys_compile_change_config(ctx, node, rfn->flags, 0, 1), cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005233 } else {
5234 LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).",
5235 flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action", ctx->path);
5236 }
Radek Krejci76b3e962018-12-14 17:01:25 +01005237 }
5238
5239 /* mandatory */
5240 if (rfn->flags & LYS_MAND_MASK) {
Radek Krejci327de162019-06-14 12:52:07 +02005241 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, node, rfn->flags, 1), cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01005242 }
Radek Krejci9a54f1f2019-01-07 13:47:55 +01005243
5244 /* presence */
5245 if (rfn->presence) {
5246 if (node->nodetype != LYS_CONTAINER) {
5247 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005248 "Invalid refine of presence statement - %s cannot hold the presence statement.",
5249 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005250 goto cleanup;
Radek Krejci9a54f1f2019-01-07 13:47:55 +01005251 }
5252 node->flags |= LYS_PRESENCE;
5253 }
Radek Krejci9a564c92019-01-07 14:53:57 +01005254
5255 /* must */
5256 if (rfn->musts) {
5257 switch (node->nodetype) {
5258 case LYS_LEAF:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005259 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 +01005260 break;
5261 case LYS_LEAFLIST:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005262 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 +01005263 break;
5264 case LYS_LIST:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005265 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 +01005266 break;
5267 case LYS_CONTAINER:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005268 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 +01005269 break;
5270 case LYS_ANYXML:
5271 case LYS_ANYDATA:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005272 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 +01005273 break;
5274 default:
5275 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005276 "Invalid refine of must statement - %s cannot hold any must statement.",
5277 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005278 goto cleanup;
Radek Krejci9a564c92019-01-07 14:53:57 +01005279 }
Michal Vasko5d8756a2019-11-07 15:21:00 +01005280 ly_set_add(&ctx->unres, node, 0);
Radek Krejci9a564c92019-01-07 14:53:57 +01005281 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01005282
5283 /* min/max-elements */
5284 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
5285 switch (node->nodetype) {
5286 case LYS_LEAFLIST:
5287 if (rfn->flags & LYS_SET_MAX) {
5288 ((struct lysc_node_leaflist*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
5289 }
5290 if (rfn->flags & LYS_SET_MIN) {
5291 ((struct lysc_node_leaflist*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01005292 if (rfn->min) {
5293 node->flags |= LYS_MAND_TRUE;
5294 lys_compile_mandatory_parents(node->parent, 1);
5295 } else {
5296 node->flags &= ~LYS_MAND_TRUE;
5297 lys_compile_mandatory_parents(node->parent, 0);
5298 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01005299 }
5300 break;
5301 case LYS_LIST:
5302 if (rfn->flags & LYS_SET_MAX) {
5303 ((struct lysc_node_list*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
5304 }
5305 if (rfn->flags & LYS_SET_MIN) {
5306 ((struct lysc_node_list*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01005307 if (rfn->min) {
5308 node->flags |= LYS_MAND_TRUE;
5309 lys_compile_mandatory_parents(node->parent, 1);
5310 } else {
5311 node->flags &= ~LYS_MAND_TRUE;
5312 lys_compile_mandatory_parents(node->parent, 0);
5313 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01005314 }
5315 break;
5316 default:
5317 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005318 "Invalid refine of %s statement - %s cannot hold this statement.",
5319 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005320 goto cleanup;
Radek Krejci6b22ab72019-01-07 15:39:20 +01005321 }
5322 }
Radek Krejcif0089082019-01-07 16:42:01 +01005323
5324 /* if-feature */
5325 if (rfn->iffeatures) {
5326 /* any node in compiled tree can get additional if-feature, so do not check nodetype */
Radek Krejciec4da802019-05-02 13:02:41 +02005327 COMPILE_ARRAY_GOTO(ctx, rfn->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcif0089082019-01-07 16:42:01 +01005328 }
Radek Krejci327de162019-06-14 12:52:07 +02005329
5330 lysc_update_path(ctx, NULL, NULL);
Radek Krejci01342af2019-01-03 15:18:08 +01005331 }
Radek Krejcie86bf772018-12-14 11:39:53 +01005332
Radek Krejcif2271f12019-01-07 16:42:23 +01005333 /* do some additional checks of the changed nodes when all the refines are applied */
5334 for (u = 0; u < refined.count; ++u) {
5335 node = (struct lysc_node*)refined.objs[u];
5336 rfn = &uses_p->refines[u];
Radek Krejci327de162019-06-14 12:52:07 +02005337 lysc_update_path(ctx, NULL, rfn->nodeid);
Radek Krejcif2271f12019-01-07 16:42:23 +01005338
5339 /* check possible conflict with default value (default added, mandatory left true) */
5340 if ((node->flags & LYS_MAND_TRUE) &&
5341 (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) ||
5342 ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) {
5343 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005344 "Invalid refine of default - the node is mandatory.");
Radek Krejcifc11bd72019-04-11 16:00:05 +02005345 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01005346 }
5347
5348 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
5349 if (node->nodetype == LYS_LIST) {
5350 min = ((struct lysc_node_list*)node)->min;
5351 max = ((struct lysc_node_list*)node)->max;
5352 } else {
5353 min = ((struct lysc_node_leaflist*)node)->min;
5354 max = ((struct lysc_node_leaflist*)node)->max;
5355 }
5356 if (min > max) {
5357 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005358 "Invalid refine of %s statement - \"min-elements\" is bigger than \"max-elements\".",
5359 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements");
Radek Krejcifc11bd72019-04-11 16:00:05 +02005360 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01005361 }
5362 }
5363 }
5364
Radek Krejci327de162019-06-14 12:52:07 +02005365 lysc_update_path(ctx, NULL, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01005366 ret = LY_SUCCESS;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005367
5368cleanup:
5369 /* fix connection of the children nodes from fake context node back into the parent */
5370 if (context_node_fake.child) {
5371 context_node_fake.child->prev = child;
5372 }
5373 LY_LIST_FOR(context_node_fake.child, child) {
5374 child->parent = parent;
5375 }
5376
5377 if (uses_p->augments || uses_p->refines) {
5378 /* return back actions and notifications in case they were separated for augment/refine processing */
Radek Krejci65e20e22019-04-12 09:44:37 +02005379 if (context_node_fake.actions) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005380 memcpy(&(*actions)[actions_index], context_node_fake.actions, LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions);
5381 LY_ARRAY_FREE(context_node_fake.actions);
5382 }
Radek Krejci65e20e22019-04-12 09:44:37 +02005383 if (context_node_fake.notifs) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005384 memcpy(&(*notifs)[notifs_index], context_node_fake.notifs, LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs);
5385 LY_ARRAY_FREE(context_node_fake.notifs);
5386 }
5387 }
5388
Radek Krejcie86bf772018-12-14 11:39:53 +01005389 /* reload previous context's mod_def */
5390 ctx->mod_def = mod_old;
5391 /* remove the grouping from the stack for circular groupings dependency check */
5392 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
5393 assert(ctx->groupings.count == grp_stack_count);
Radek Krejcif2271f12019-01-07 16:42:23 +01005394 ly_set_erase(&refined, NULL);
Radek Krejci3641f562019-02-13 15:38:40 +01005395 LY_ARRAY_FREE(augments);
Radek Krejcie86bf772018-12-14 11:39:53 +01005396
5397 return ret;
5398}
5399
Radek Krejci327de162019-06-14 12:52:07 +02005400static int
5401lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path)
5402{
5403 struct lysp_node *iter;
5404 int len = 0;
5405
5406 *path = NULL;
5407 for (iter = node; iter && len >= 0; iter = iter->parent) {
5408 char *s = *path;
5409 char *id;
5410
5411 switch (iter->nodetype) {
5412 case LYS_USES:
5413 asprintf(&id, "{uses='%s'}", iter->name);
5414 break;
5415 case LYS_GROUPING:
5416 asprintf(&id, "{grouping='%s'}", iter->name);
5417 break;
5418 case LYS_AUGMENT:
5419 asprintf(&id, "{augment='%s'}", iter->name);
5420 break;
5421 default:
5422 id = strdup(iter->name);
5423 break;
5424 }
5425
5426 if (!iter->parent) {
5427 /* print prefix */
5428 len = asprintf(path, "/%s:%s%s", ctx->mod->name, id, s ? s : "");
5429 } else {
5430 /* prefix is the same as in parent */
5431 len = asprintf(path, "/%s%s", id, s ? s : "");
5432 }
5433 free(s);
5434 free(id);
5435 }
5436
5437 if (len < 0) {
5438 free(*path);
5439 *path = NULL;
5440 } else if (len == 0) {
5441 *path = strdup("/");
5442 len = 1;
5443 }
5444 return len;
5445}
5446
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005447/**
5448 * @brief Validate groupings that were defined but not directly used in the schema itself.
5449 *
5450 * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately),
5451 * but to have the complete result of the schema validity, even such groupings are supposed to be checked.
5452 */
5453static LY_ERR
5454lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysp_grp *grp)
5455{
5456 LY_ERR ret;
Radek Krejci327de162019-06-14 12:52:07 +02005457 char *path;
5458 int len;
5459
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005460 struct lysp_node_uses fake_uses = {
5461 .parent = node_p,
5462 .nodetype = LYS_USES,
5463 .flags = 0, .next = NULL,
5464 .name = grp->name,
5465 .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL,
5466 .refines = NULL, .augments = NULL
5467 };
5468 struct lysc_node_container fake_container = {
5469 .nodetype = LYS_CONTAINER,
5470 .flags = node_p ? (node_p->flags & LYS_FLAGS_COMPILED_MASK) : 0,
5471 .module = ctx->mod,
5472 .sp = NULL, .parent = NULL, .next = NULL,
5473 .prev = (struct lysc_node*)&fake_container,
5474 .name = "fake",
5475 .dsc = NULL, .ref = NULL, .exts = NULL, .iffeatures = NULL, .when = NULL,
5476 .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL
5477 };
5478
5479 if (grp->parent) {
5480 LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name);
5481 }
Radek Krejci327de162019-06-14 12:52:07 +02005482
5483 len = lys_compile_grouping_pathlog(ctx, grp->parent, &path);
5484 if (len < 0) {
5485 LOGMEM(ctx->ctx);
5486 return LY_EMEM;
5487 }
5488 strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1);
5489 ctx->path_len = (uint16_t)len;
5490 free(path);
5491
5492 lysc_update_path(ctx, NULL, "{grouping}");
5493 lysc_update_path(ctx, NULL, grp->name);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005494 ret = lys_compile_uses(ctx, &fake_uses, (struct lysc_node*)&fake_container);
Radek Krejci327de162019-06-14 12:52:07 +02005495 lysc_update_path(ctx, NULL, NULL);
5496 lysc_update_path(ctx, NULL, NULL);
5497
5498 ctx->path_len = 1;
5499 ctx->path[1] = '\0';
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005500
5501 /* cleanup */
5502 lysc_node_container_free(ctx->ctx, &fake_container);
5503
5504 return ret;
5505}
Radek Krejcife909632019-02-12 15:34:42 +01005506
Radek Krejcie86bf772018-12-14 11:39:53 +01005507/**
Radek Krejcia3045382018-11-22 14:30:31 +01005508 * @brief Compile parsed schema node information.
5509 * @param[in] ctx Compile context
5510 * @param[in] node_p Parsed schema node.
Radek Krejcia3045382018-11-22 14:30:31 +01005511 * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is
5512 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
5513 * the compile context.
Radek Krejcib1b59152019-01-07 13:21:56 +01005514 * @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).
5515 * Zero means no uses, non-zero value with no status bit set mean the default status.
Radek Krejcia3045382018-11-22 14:30:31 +01005516 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
5517 */
Radek Krejci19a96102018-11-15 13:38:09 +01005518static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005519lys_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 +01005520{
5521 LY_ERR ret = LY_EVALID;
Radek Krejci056d0a82018-12-06 16:57:25 +01005522 struct lysc_node *node;
5523 struct lysc_node_case *cs;
Radek Krejci00b874b2019-02-12 10:54:50 +01005524 struct lysc_when **when;
Radek Krejci19a96102018-11-15 13:38:09 +01005525 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02005526 LY_ERR (*node_compile_spec)(struct lysc_ctx*, struct lysp_node*, struct lysc_node*);
Radek Krejci19a96102018-11-15 13:38:09 +01005527
Radek Krejci327de162019-06-14 12:52:07 +02005528 if (node_p->nodetype != LYS_USES) {
5529 lysc_update_path(ctx, parent, node_p->name);
5530 } else {
5531 lysc_update_path(ctx, NULL, "{uses}");
5532 lysc_update_path(ctx, NULL, node_p->name);
5533 }
5534
Radek Krejci19a96102018-11-15 13:38:09 +01005535 switch (node_p->nodetype) {
5536 case LYS_CONTAINER:
5537 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container));
5538 node_compile_spec = lys_compile_node_container;
5539 break;
5540 case LYS_LEAF:
5541 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf));
5542 node_compile_spec = lys_compile_node_leaf;
5543 break;
5544 case LYS_LIST:
5545 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005546 node_compile_spec = lys_compile_node_list;
Radek Krejci19a96102018-11-15 13:38:09 +01005547 break;
5548 case LYS_LEAFLIST:
5549 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist));
Radek Krejci0e5d8382018-11-28 16:37:53 +01005550 node_compile_spec = lys_compile_node_leaflist;
Radek Krejci19a96102018-11-15 13:38:09 +01005551 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005552 case LYS_CHOICE:
5553 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice));
Radek Krejci056d0a82018-12-06 16:57:25 +01005554 node_compile_spec = lys_compile_node_choice;
Radek Krejci19a96102018-11-15 13:38:09 +01005555 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005556 case LYS_ANYXML:
5557 case LYS_ANYDATA:
5558 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata));
Radek Krejci9800fb82018-12-13 14:26:23 +01005559 node_compile_spec = lys_compile_node_any;
Radek Krejci19a96102018-11-15 13:38:09 +01005560 break;
Radek Krejcie86bf772018-12-14 11:39:53 +01005561 case LYS_USES:
Radek Krejci327de162019-06-14 12:52:07 +02005562 ret = lys_compile_uses(ctx, (struct lysp_node_uses*)node_p, parent);
5563 lysc_update_path(ctx, NULL, NULL);
5564 lysc_update_path(ctx, NULL, NULL);
5565 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01005566 default:
5567 LOGINT(ctx->ctx);
5568 return LY_EINT;
5569 }
5570 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
5571 node->nodetype = node_p->nodetype;
5572 node->module = ctx->mod;
5573 node->prev = node;
Radek Krejci0e5d8382018-11-28 16:37:53 +01005574 node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01005575
5576 /* config */
Radek Krejciec4da802019-05-02 13:02:41 +02005577 if (ctx->options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005578 /* ignore config statements inside RPC/action data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005579 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejciec4da802019-05-02 13:02:41 +02005580 node->flags |= (ctx->options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R;
5581 } else if (ctx->options & LYSC_OPT_NOTIFICATION) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005582 /* ignore config statements inside Notification data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005583 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005584 node->flags |= LYS_CONFIG_R;
5585 } else if (!(node->flags & LYS_CONFIG_MASK)) {
Radek Krejci19a96102018-11-15 13:38:09 +01005586 /* config not explicitely set, inherit it from parent */
5587 if (parent) {
5588 node->flags |= parent->flags & LYS_CONFIG_MASK;
5589 } else {
5590 /* default is config true */
5591 node->flags |= LYS_CONFIG_W;
5592 }
Radek Krejci93dcc392019-02-19 10:43:38 +01005593 } else {
5594 /* config set explicitely */
5595 node->flags |= LYS_SET_CONFIG;
Radek Krejci19a96102018-11-15 13:38:09 +01005596 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005597 if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
5598 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5599 "Configuration node cannot be child of any state data node.");
5600 goto error;
5601 }
Radek Krejci19a96102018-11-15 13:38:09 +01005602
Radek Krejcia6d57732018-11-29 13:40:37 +01005603 /* *list ordering */
5604 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
5605 if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005606 LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing %s (%s).",
Radek Krejciec4da802019-05-02 13:02:41 +02005607 (ctx->options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" :
5608 (ctx->options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path);
Radek Krejcia6d57732018-11-29 13:40:37 +01005609 node->flags &= ~LYS_ORDBY_MASK;
5610 node->flags |= LYS_ORDBY_SYSTEM;
5611 } else if (!(node->flags & LYS_ORDBY_MASK)) {
5612 /* default ordering is system */
5613 node->flags |= LYS_ORDBY_SYSTEM;
5614 }
5615 }
5616
Radek Krejci19a96102018-11-15 13:38:09 +01005617 /* status - it is not inherited by specification, but it does not make sense to have
5618 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Radek Krejci056d0a82018-12-06 16:57:25 +01005619 if (!parent || parent->nodetype != LYS_CHOICE) {
5620 /* in case of choice/case's children, postpone the check to the moment we know if
5621 * 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 +01005622 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 +01005623 }
5624
Radek Krejciec4da802019-05-02 13:02:41 +02005625 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci19a96102018-11-15 13:38:09 +01005626 node->sp = node_p;
5627 }
5628 DUP_STRING(ctx->ctx, node_p->name, node->name);
Radek Krejci12fb9142019-01-08 09:45:30 +01005629 DUP_STRING(ctx->ctx, node_p->dsc, node->dsc);
5630 DUP_STRING(ctx->ctx, node_p->ref, node->ref);
Radek Krejci00b874b2019-02-12 10:54:50 +01005631 if (node_p->when) {
5632 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01005633 ret = lys_compile_when(ctx, node_p->when, node_p->flags, node, when);
Radek Krejci00b874b2019-02-12 10:54:50 +01005634 LY_CHECK_GOTO(ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01005635
5636 if (!(ctx->options & LYSC_OPT_GROUPING)) {
5637 /* do not check "when" semantics in a grouping */
5638 ly_set_add(&ctx->unres, node, 0);
5639 }
Radek Krejci00b874b2019-02-12 10:54:50 +01005640 }
Radek Krejciec4da802019-05-02 13:02:41 +02005641 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01005642
5643 /* nodetype-specific part */
Radek Krejciec4da802019-05-02 13:02:41 +02005644 LY_CHECK_GOTO(node_compile_spec(ctx, node_p, node), error);
Radek Krejci19a96102018-11-15 13:38:09 +01005645
Radek Krejci0935f412019-08-20 16:15:18 +02005646 COMPILE_EXTS_GOTO(ctx, node_p->exts, node->exts, node, LYEXT_PAR_NODE, ret, error);
5647
Radek Krejcife909632019-02-12 15:34:42 +01005648 /* inherit LYS_MAND_TRUE in parent containers */
5649 if (node->flags & LYS_MAND_TRUE) {
5650 lys_compile_mandatory_parents(parent, 1);
5651 }
5652
Radek Krejci327de162019-06-14 12:52:07 +02005653 lysc_update_path(ctx, NULL, NULL);
5654
Radek Krejci19a96102018-11-15 13:38:09 +01005655 /* insert into parent's children */
Radek Krejcia3045382018-11-22 14:30:31 +01005656 if (parent) {
5657 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci327de162019-06-14 12:52:07 +02005658 if (node_p->parent->nodetype == LYS_CASE) {
5659 lysc_update_path(ctx, parent, node_p->parent->name);
5660 } else {
5661 lysc_update_path(ctx, parent, node->name);
5662 }
Radek Krejciec4da802019-05-02 13:02:41 +02005663 cs = lys_compile_node_case(ctx, node_p->parent, (struct lysc_node_choice*)parent, node);
Radek Krejci056d0a82018-12-06 16:57:25 +01005664 LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error);
Radek Krejcib1b59152019-01-07 13:21:56 +01005665 if (uses_status) {
5666
5667 }
Radek Krejci056d0a82018-12-06 16:57:25 +01005668 /* the postponed status check of the node and its real parent - in case of implicit case,
Radek Krejcib1b59152019-01-07 13:21:56 +01005669 * it directly gets the same status flags as the choice;
5670 * uses_status cannot be applied here since uses cannot be child statement of choice */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005671 LY_CHECK_GOTO(lys_compile_status(ctx, &node->flags, cs->flags), error);
Radek Krejci056d0a82018-12-06 16:57:25 +01005672 node->parent = (struct lysc_node*)cs;
Radek Krejci327de162019-06-14 12:52:07 +02005673 lysc_update_path(ctx, parent, node->name);
Radek Krejci056d0a82018-12-06 16:57:25 +01005674 } else { /* other than choice */
Radek Krejci327de162019-06-14 12:52:07 +02005675 lysc_update_path(ctx, parent, node->name);
Radek Krejci056d0a82018-12-06 16:57:25 +01005676 node->parent = parent;
Radek Krejci19a96102018-11-15 13:38:09 +01005677 }
Radek Krejciec4da802019-05-02 13:02:41 +02005678 LY_CHECK_RET(lys_compile_node_connect(ctx, parent->nodetype == LYS_CASE ? parent->parent : parent, node), LY_EVALID);
Radek Krejci327de162019-06-14 12:52:07 +02005679
5680 if (parent->nodetype == LYS_CHOICE) {
5681 lysc_update_path(ctx, NULL, NULL);
5682 }
Radek Krejci19a96102018-11-15 13:38:09 +01005683 } else {
5684 /* top-level element */
5685 if (!ctx->mod->compiled->data) {
5686 ctx->mod->compiled->data = node;
5687 } else {
5688 /* insert at the end of the module's top-level nodes list */
5689 ctx->mod->compiled->data->prev->next = node;
5690 node->prev = ctx->mod->compiled->data->prev;
5691 ctx->mod->compiled->data->prev = node;
5692 }
Radek Krejci327de162019-06-14 12:52:07 +02005693 lysc_update_path(ctx, parent, node->name);
Radek Krejci76b3e962018-12-14 17:01:25 +01005694 if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs,
5695 ctx->mod->compiled->notifs, node->name, node)) {
5696 return LY_EVALID;
5697 }
Radek Krejci19a96102018-11-15 13:38:09 +01005698 }
Radek Krejci327de162019-06-14 12:52:07 +02005699 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01005700
5701 return LY_SUCCESS;
5702
5703error:
5704 lysc_node_free(ctx->ctx, node);
5705 return ret;
5706}
5707
Radek Krejciccd20f12019-02-15 14:12:27 +01005708static void
5709lysc_disconnect(struct lysc_node *node)
5710{
Radek Krejci0a048dd2019-02-18 16:01:43 +01005711 struct lysc_node *parent, *child, *prev = NULL, *next;
5712 struct lysc_node_case *cs = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01005713 int remove_cs = 0;
5714
5715 parent = node->parent;
5716
5717 /* parent's first child */
5718 if (!parent) {
5719 return;
5720 }
5721 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci0a048dd2019-02-18 16:01:43 +01005722 cs = (struct lysc_node_case*)node;
5723 } else if (parent->nodetype == LYS_CASE) {
5724 /* disconnecting some node in a case */
5725 cs = (struct lysc_node_case*)parent;
5726 parent = cs->parent;
5727 for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) {
5728 if (child == node) {
5729 if (cs->child == child) {
5730 if (!child->next || child->next->parent != (struct lysc_node*)cs) {
5731 /* case with a single child -> remove also the case */
5732 child->parent = NULL;
5733 remove_cs = 1;
5734 } else {
5735 cs->child = child->next;
Radek Krejciccd20f12019-02-15 14:12:27 +01005736 }
5737 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005738 break;
Radek Krejciccd20f12019-02-15 14:12:27 +01005739 }
5740 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005741 if (!remove_cs) {
5742 cs = NULL;
5743 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005744 } else if (lysc_node_children(parent, node->flags) == node) {
5745 *lysc_node_children_p(parent, node->flags) = node->next;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005746 }
5747
5748 if (cs) {
5749 if (remove_cs) {
5750 /* cs has only one child which is being also removed */
5751 lysc_disconnect((struct lysc_node*)cs);
5752 lysc_node_free(cs->module->ctx, (struct lysc_node*)cs);
5753 } else {
Radek Krejciccd20f12019-02-15 14:12:27 +01005754 if (((struct lysc_node_choice*)parent)->dflt == cs) {
5755 /* default case removed */
5756 ((struct lysc_node_choice*)parent)->dflt = NULL;
5757 }
5758 if (((struct lysc_node_choice*)parent)->cases == cs) {
5759 /* first case removed */
5760 ((struct lysc_node_choice*)parent)->cases = (struct lysc_node_case*)cs->next;
5761 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005762 if (cs->child) {
5763 /* cs will be removed and disconnected from its siblings, but we have to take care also about its children */
5764 if (cs->child->prev->parent != (struct lysc_node*)cs) {
5765 prev = cs->child->prev;
5766 } /* else all the children are under a single case */
5767 LY_LIST_FOR_SAFE(cs->child, next, child) {
5768 if (child->parent != (struct lysc_node*)cs) {
5769 break;
5770 }
5771 lysc_node_free(node->module->ctx, child);
5772 }
5773 if (prev) {
5774 if (prev->next) {
5775 prev->next = child;
5776 }
5777 if (child) {
5778 child->prev = prev;
5779 } else {
5780 /* link from the first child under the cases */
5781 ((struct lysc_node_choice*)cs->parent)->cases->child->prev = prev;
5782 }
5783 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005784 }
5785 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005786 }
5787
5788 /* siblings */
Radek Krejci0a048dd2019-02-18 16:01:43 +01005789 if (node->prev->next) {
5790 node->prev->next = node->next;
5791 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005792 if (node->next) {
5793 node->next->prev = node->prev;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005794 } else if (node->nodetype != LYS_CASE) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005795 child = (struct lysc_node*)lysc_node_children(parent, node->flags);
Radek Krejci0a048dd2019-02-18 16:01:43 +01005796 if (child) {
5797 child->prev = node->prev;
5798 }
5799 } else if (((struct lysc_node_choice*)parent)->cases) {
5800 ((struct lysc_node_choice*)parent)->cases->prev = node->prev;
Radek Krejciccd20f12019-02-15 14:12:27 +01005801 }
5802}
5803
5804LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005805lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p)
Radek Krejciccd20f12019-02-15 14:12:27 +01005806{
Radek Krejcia1911222019-07-22 17:24:50 +02005807 LY_ERR ret = LY_EVALID, rc;
Radek Krejciccd20f12019-02-15 14:12:27 +01005808 struct ly_set devs_p = {0};
5809 struct ly_set targets = {0};
5810 struct lysc_node *target; /* target target of the deviation */
Radek Krejci7af64242019-02-18 13:07:53 +01005811 struct lysc_node_list *list;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005812 struct lysc_action *rpcs;
5813 struct lysc_notif *notifs;
Radek Krejciccd20f12019-02-15 14:12:27 +01005814 struct lysp_deviation *dev;
5815 struct lysp_deviate *d, **dp_new;
5816 struct lysp_deviate_add *d_add;
5817 struct lysp_deviate_del *d_del;
5818 struct lysp_deviate_rpl *d_rpl;
Radek Krejci7af64242019-02-18 13:07:53 +01005819 unsigned int u, v, x, y, z;
Radek Krejciccd20f12019-02-15 14:12:27 +01005820 struct lysc_deviation {
5821 const char *nodeid;
5822 struct lysc_node *target; /* target node of the deviation */
5823 struct lysp_deviate** deviates;/* sized array of pointers to parsed deviate statements to apply on target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005824 uint16_t flags; /* target's flags from lys_resolve_schema_nodeid() */
Radek Krejciccd20f12019-02-15 14:12:27 +01005825 uint8_t not_supported; /* flag if deviates contains not-supported deviate */
5826 } **devs = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02005827 int i, changed_type;
Radek Krejciccd20f12019-02-15 14:12:27 +01005828 size_t prefix_len, name_len;
Radek Krejcia1911222019-07-22 17:24:50 +02005829 const char *prefix, *name, *nodeid, *dflt;
Radek Krejciccd20f12019-02-15 14:12:27 +01005830 struct lys_module *mod;
Radek Krejci551b12c2019-02-19 16:11:21 +01005831 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005832 uint16_t flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005833
5834 /* get all deviations from the module and all its submodules ... */
5835 LY_ARRAY_FOR(mod_p->deviations, u) {
5836 ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST);
5837 }
5838 LY_ARRAY_FOR(mod_p->includes, v) {
5839 LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) {
5840 ly_set_add(&devs_p, &mod_p->includes[v].submodule->deviations[u], LY_SET_OPT_USEASLIST);
5841 }
5842 }
5843 if (!devs_p.count) {
5844 /* nothing to do */
5845 return LY_SUCCESS;
5846 }
5847
Radek Krejci327de162019-06-14 12:52:07 +02005848 lysc_update_path(ctx, NULL, "{deviation}");
5849
Radek Krejciccd20f12019-02-15 14:12:27 +01005850 /* ... and group them by the target node */
5851 devs = calloc(devs_p.count, sizeof *devs);
5852 for (u = 0; u < devs_p.count; ++u) {
5853 dev = devs_p.objs[u];
Radek Krejci327de162019-06-14 12:52:07 +02005854 lysc_update_path(ctx, NULL, dev->nodeid);
Radek Krejciccd20f12019-02-15 14:12:27 +01005855
5856 /* resolve the target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005857 LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1,
5858 (const struct lysc_node**)&target, &flags), cleanup);
Radek Krejcif538ce52019-03-05 10:46:14 +01005859 if (target->nodetype == LYS_ACTION) {
5860 /* move the target pointer to input/output to make them different from the action and
5861 * between them. Before the devs[] item is being processed, the target pointer must be fixed
5862 * back to the RPC/action node due to a better compatibility and decision code in this function.
5863 * The LYSC_OPT_INTERNAL is used as a flag to this change. */
5864 if (flags & LYSC_OPT_RPC_INPUT) {
5865 target = (struct lysc_node*)&((struct lysc_action*)target)->input;
5866 flags |= LYSC_OPT_INTERNAL;
5867 } else if (flags & LYSC_OPT_RPC_OUTPUT) {
5868 target = (struct lysc_node*)&((struct lysc_action*)target)->output;
5869 flags |= LYSC_OPT_INTERNAL;
5870 }
5871 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005872 /* insert into the set of targets with duplicity detection */
5873 i = ly_set_add(&targets, target, 0);
5874 if (!devs[i]) {
5875 /* new record */
5876 devs[i] = calloc(1, sizeof **devs);
5877 devs[i]->target = target;
5878 devs[i]->nodeid = dev->nodeid;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005879 devs[i]->flags = flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005880 }
5881 /* add deviates into the deviation's list of deviates */
5882 for (d = dev->deviates; d; d = d->next) {
5883 LY_ARRAY_NEW_GOTO(ctx->ctx, devs[i]->deviates, dp_new, ret, cleanup);
5884 *dp_new = d;
5885 if (d->mod == LYS_DEV_NOT_SUPPORTED) {
5886 devs[i]->not_supported = 1;
5887 }
5888 }
Radek Krejci327de162019-06-14 12:52:07 +02005889
5890 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01005891 }
5892
5893 /* MACROS for deviates checking */
5894#define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \
5895 if (!(devs[u]->target->nodetype & (NODETYPES))) { \
Radek Krejci327de162019-06-14 12:52:07 +02005896 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, lys_nodetype2str(devs[u]->target->nodetype), DEVTYPE, PROPERTY);\
Radek Krejciccd20f12019-02-15 14:12:27 +01005897 goto cleanup; \
5898 }
5899
5900#define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \
5901 if (LY_ARRAY_SIZE(ARRAY) > MAX) { \
Radek Krejci327de162019-06-14 12:52:07 +02005902 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation of %s with too many (%u) %s properties.", \
5903 lys_nodetype2str(devs[u]->target->nodetype), LY_ARRAY_SIZE(ARRAY), PROPERTY); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005904 goto cleanup; \
5905 }
5906
Radek Krejcia1911222019-07-22 17:24:50 +02005907
Radek Krejciccd20f12019-02-15 14:12:27 +01005908#define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \
Radek Krejcia1911222019-07-22 17:24:50 +02005909 if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \
5910 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5911 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \
5912 PROPERTY, ((TYPE)devs[u]->target)->VALUEMEMBER); \
5913 goto cleanup; \
5914 }
5915
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005916#define DEV_CHECK_NONPRESENCE_VALUE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER, VALUEMODMEMBER) \
Radek Krejci93dcc392019-02-19 10:43:38 +01005917 if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \
Radek Krejcia1911222019-07-22 17:24:50 +02005918 int dynamic_ = 0; const char *val_; \
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005919 val_ = ((TYPE)devs[u]->target)->VALUEMEMBER->realtype->plugin->print(((TYPE)devs[u]->target)->VALUEMEMBER, LYD_XML, \
5920 lys_get_prefix, ((TYPE)devs[u]->target)->VALUEMODMEMBER, &dynamic_); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005921 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejcia1911222019-07-22 17:24:50 +02005922 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", PROPERTY, val_); \
5923 if (dynamic_) {free((void*)val_);} \
Radek Krejciccd20f12019-02-15 14:12:27 +01005924 goto cleanup; \
5925 }
5926
Radek Krejci551b12c2019-02-19 16:11:21 +01005927#define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5928 if (((TYPE)devs[u]->target)->MEMBER COND) { \
5929 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005930 "Invalid deviation adding \"%s\" property which already exists (with value \"%u\").", \
5931 PROPERTY, ((TYPE)devs[u]->target)->MEMBER); \
Radek Krejci551b12c2019-02-19 16:11:21 +01005932 goto cleanup; \
5933 }
5934
Radek Krejciccd20f12019-02-15 14:12:27 +01005935#define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \
5936 if (!((TYPE)devs[u]->target)->MEMBER || COND) { \
Radek Krejci327de162019-06-14 12:52:07 +02005937 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005938 goto cleanup; \
5939 }
5940
Radek Krejci551b12c2019-02-19 16:11:21 +01005941#define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5942 if (!(((TYPE)devs[u]->target)->MEMBER COND)) { \
5943 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005944 "Invalid deviation replacing with \"%s\" property \"%u\" which is not present.", PROPERTY, d_rpl->MEMBER); \
Radek Krejci551b12c2019-02-19 16:11:21 +01005945 goto cleanup; \
5946 }
5947
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005948#define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \
5949 DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d_del->ARRAY_DEV[0]VALMEMBER); \
5950 LY_ARRAY_FOR(d_del->ARRAY_DEV, x) { \
5951 LY_ARRAY_FOR(((TYPE)devs[u]->target)->ARRAY_TRG, y) { \
5952 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 +01005953 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005954 if (y == LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
Radek Krejciccd20f12019-02-15 14:12:27 +01005955 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005956 "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \
5957 PROPERTY, d_del->ARRAY_DEV[x]VALMEMBER); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005958 goto cleanup; \
5959 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005960 LY_ARRAY_DECREMENT(((TYPE)devs[u]->target)->ARRAY_TRG); \
5961 DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)devs[u]->target)->ARRAY_TRG[y]); \
5962 memmove(&((TYPE)devs[u]->target)->ARRAY_TRG[y], \
5963 &((TYPE)devs[u]->target)->ARRAY_TRG[y + 1], \
5964 (LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG) - y) * (sizeof *((TYPE)devs[u]->target)->ARRAY_TRG)); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005965 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005966 if (!LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
5967 LY_ARRAY_FREE(((TYPE)devs[u]->target)->ARRAY_TRG); \
5968 ((TYPE)devs[u]->target)->ARRAY_TRG = NULL; \
Radek Krejciccd20f12019-02-15 14:12:27 +01005969 }
5970
5971 /* apply deviations */
5972 for (u = 0; u < devs_p.count && devs[u]; ++u) {
Radek Krejcia1911222019-07-22 17:24:50 +02005973 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)devs[u]->target;
5974 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)devs[u]->target;
5975 struct ly_err_item *err = NULL;
5976
5977 dflt = NULL;
5978 changed_type = 0;
5979
Radek Krejci327de162019-06-14 12:52:07 +02005980 lysc_update_path(ctx, NULL, devs[u]->nodeid);
5981
Radek Krejcif538ce52019-03-05 10:46:14 +01005982 if (devs[u]->flags & LYSC_OPT_INTERNAL) {
5983 /* fix the target pointer in case of RPC's/action's input/output */
5984 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5985 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, input));
5986 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5987 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, output));
5988 }
5989 }
5990
Radek Krejciccd20f12019-02-15 14:12:27 +01005991 /* not-supported */
5992 if (devs[u]->not_supported) {
5993 if (LY_ARRAY_SIZE(devs[u]->deviates) > 1) {
5994 LOGWRN(ctx->ctx, "Useless multiple (%u) deviates on node \"%s\" since the node is not-supported.",
5995 LY_ARRAY_SIZE(devs[u]->deviates), devs[u]->nodeid);
5996 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02005997
5998#define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \
5999 if (devs[u]->target->parent) { \
6000 ARRAY = (TYPE*)GETFUNC(devs[u]->target->parent); \
6001 } else { \
6002 ARRAY = devs[u]->target->module->compiled->ARRAY; \
6003 } \
6004 LY_ARRAY_FOR(ARRAY, x) { \
6005 if (&ARRAY[x] == (TYPE*)devs[u]->target) { break; } \
6006 } \
6007 if (x < LY_ARRAY_SIZE(ARRAY)) { \
6008 FREEFUNC(ctx->ctx, &ARRAY[x]); \
6009 memmove(&ARRAY[x], &ARRAY[x + 1], (LY_ARRAY_SIZE(ARRAY) - (x + 1)) * sizeof *ARRAY); \
6010 LY_ARRAY_DECREMENT(ARRAY); \
6011 }
6012
Radek Krejcif538ce52019-03-05 10:46:14 +01006013 if (devs[u]->target->nodetype == LYS_ACTION) {
6014 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
6015 /* remove RPC's/action's input */
6016 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->input);
6017 memset(&((struct lysc_action*)devs[u]->target)->input, 0, sizeof ((struct lysc_action*)devs[u]->target)->input);
Radek Krejcifc11bd72019-04-11 16:00:05 +02006018 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->input_exts, lysc_ext_instance_free);
6019 ((struct lysc_action*)devs[u]->target)->input_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01006020 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
6021 /* remove RPC's/action's output */
6022 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->output);
6023 memset(&((struct lysc_action*)devs[u]->target)->output, 0, sizeof ((struct lysc_action*)devs[u]->target)->output);
Radek Krejcifc11bd72019-04-11 16:00:05 +02006024 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->output_exts, lysc_ext_instance_free);
6025 ((struct lysc_action*)devs[u]->target)->output_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01006026 } else {
6027 /* remove RPC/action */
Radek Krejcifc11bd72019-04-11 16:00:05 +02006028 REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01006029 }
6030 } else if (devs[u]->target->nodetype == LYS_NOTIF) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02006031 /* remove Notification */
6032 REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01006033 } else {
6034 /* remove the target node */
6035 lysc_disconnect(devs[u]->target);
6036 lysc_node_free(ctx->ctx, devs[u]->target);
6037 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006038
Radek Krejci474f9b82019-07-24 11:36:37 +02006039 /* mark the context for later re-compilation of objects that could reference the curently removed node */
6040 ctx->ctx->flags |= LY_CTX_CHANGED_TREE;
Radek Krejciccd20f12019-02-15 14:12:27 +01006041 continue;
6042 }
6043
6044 /* list of deviates (not-supported is not present in the list) */
6045 LY_ARRAY_FOR(devs[u]->deviates, v) {
6046 d = devs[u]->deviates[v];
6047
6048 switch (d->mod) {
6049 case LYS_DEV_ADD:
6050 d_add = (struct lysp_deviate_add*)d;
6051 /* [units-stmt] */
6052 if (d_add->units) {
6053 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units");
6054 DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_UNITS), units, "units", units);
6055
6056 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
6057 DUP_STRING(ctx->ctx, d_add->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
6058 }
6059
6060 /* *must-stmt */
6061 if (d_add->musts) {
6062 switch (devs[u]->target->nodetype) {
6063 case LYS_CONTAINER:
6064 case LYS_LIST:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02006065 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_container*)devs[u]->target)->musts,
6066 x, lys_compile_must, ret, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01006067 break;
6068 case LYS_LEAF:
6069 case LYS_LEAFLIST:
6070 case LYS_ANYDATA:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02006071 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_leaf*)devs[u]->target)->musts,
6072 x, lys_compile_must, ret, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01006073 break;
6074 case LYS_NOTIF:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02006075 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_notif*)devs[u]->target)->musts,
6076 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006077 break;
6078 case LYS_ACTION:
6079 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
Radek Krejcic71ac5b2019-09-10 15:34:22 +02006080 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->input.musts,
6081 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006082 break;
6083 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
Radek Krejcic71ac5b2019-09-10 15:34:22 +02006084 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->output.musts,
6085 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006086 break;
6087 }
6088 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01006089 default:
6090 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006091 lys_nodetype2str(devs[u]->target->nodetype), "add", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01006092 goto cleanup;
6093 }
Michal Vasko5d8756a2019-11-07 15:21:00 +01006094 ly_set_add(&ctx->unres, devs[u]->target, 0);
Radek Krejciccd20f12019-02-15 14:12:27 +01006095 }
6096
6097 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01006098 if (d_add->uniques) {
6099 DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique");
6100 LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d_add->uniques, (struct lysc_node_list*)devs[u]->target), cleanup);
6101 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006102
6103 /* *default-stmt */
6104 if (d_add->dflts) {
Radek Krejciccd20f12019-02-15 14:12:27 +01006105 switch (devs[u]->target->nodetype) {
6106 case LYS_LEAF:
6107 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006108 DEV_CHECK_NONPRESENCE_VALUE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_DFLT), dflt, "default", dflt, dflt_mod);
Radek Krejcia1911222019-07-22 17:24:50 +02006109 if (leaf->dflt) {
Radek Krejciccd20f12019-02-15 14:12:27 +01006110 /* first, remove the default value taken from the type */
Radek Krejci474f9b82019-07-24 11:36:37 +02006111 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02006112 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6113 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6114 } else {
6115 /* prepare new default value storage */
6116 leaf->dflt = calloc(1, sizeof *leaf->dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01006117 }
Radek Krejcia1911222019-07-22 17:24:50 +02006118 dflt = d_add->dflts[0];
6119 /* parsing is done at the end after possible replace of the leaf's type */
6120
Radek Krejci551b12c2019-02-19 16:11:21 +01006121 /* mark the new default values as leaf's own */
6122 devs[u]->target->flags |= LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01006123 break;
6124 case LYS_LEAFLIST:
Radek Krejcia1911222019-07-22 17:24:50 +02006125 if (llist->dflts && !(devs[u]->target->flags & LYS_SET_DFLT)) {
Radek Krejciccd20f12019-02-15 14:12:27 +01006126 /* first, remove the default value taken from the type */
Radek Krejcia1911222019-07-22 17:24:50 +02006127 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejci474f9b82019-07-24 11:36:37 +02006128 lysc_incomplete_dflts_remove(ctx, llist->dflts[x]);
Radek Krejcia1911222019-07-22 17:24:50 +02006129 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6130 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6131 free(llist->dflts[x]);
Radek Krejciccd20f12019-02-15 14:12:27 +01006132 }
Radek Krejcia1911222019-07-22 17:24:50 +02006133 LY_ARRAY_FREE(llist->dflts);
6134 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006135 LY_ARRAY_FREE(llist->dflts_mods);
6136 llist->dflts_mods = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01006137 }
6138 /* add new default value(s) */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006139 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_SIZE(d_add->dflts), ret, cleanup);
Radek Krejcia1911222019-07-22 17:24:50 +02006140 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(d_add->dflts), ret, cleanup);
6141 for (x = y = LY_ARRAY_SIZE(llist->dflts);
Radek Krejciccd20f12019-02-15 14:12:27 +01006142 x < LY_ARRAY_SIZE(d_add->dflts) + y; ++x) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006143 LY_ARRAY_INCREMENT(llist->dflts_mods);
6144 llist->dflts_mods[x] = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02006145 LY_ARRAY_INCREMENT(llist->dflts);
6146 llist->dflts[x] = calloc(1, sizeof *llist->dflts[x]);
6147 llist->dflts[x]->realtype = llist->type;
6148 rc = llist->type->plugin->store(ctx->ctx, llist->type, d_add->dflts[x - y], strlen(d_add->dflts[x - y]),
Radek Krejci474f9b82019-07-24 11:36:37 +02006149 LY_TYPE_OPTS_INCOMPLETE_DATA |LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE, lys_resolve_prefix,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006150 (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL, llist->dflts[x], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006151 llist->dflts[x]->realtype->refcount++;
6152 if (err) {
6153 ly_err_print(err);
6154 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6155 "Invalid deviation adding \"default\" property \"%s\" which does not fit the type (%s).",
6156 d_add->dflts[x - y], err->msg);
6157 ly_err_free(err);
6158 }
Radek Krejci474f9b82019-07-24 11:36:37 +02006159 if (rc == LY_EINCOMPLETE) {
6160 /* postpone default compilation when the tree is complete */
6161 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup);
6162
6163 /* but in general result is so far ok */
6164 rc = LY_SUCCESS;
6165 }
Radek Krejcia1911222019-07-22 17:24:50 +02006166 LY_CHECK_GOTO(rc, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01006167 }
Radek Krejci551b12c2019-02-19 16:11:21 +01006168 /* mark the new default values as leaf-list's own */
Radek Krejciccd20f12019-02-15 14:12:27 +01006169 devs[u]->target->flags |= LYS_SET_DFLT;
6170 break;
6171 case LYS_CHOICE:
6172 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
6173 DEV_CHECK_NONPRESENCE(struct lysc_node_choice*, 1, dflt, "default", dflt->name);
6174 /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation
6175 * to allow making the default case even the augmented case from the deviating module */
Radek Krejci327de162019-06-14 12:52:07 +02006176 if (lys_compile_deviation_set_choice_dflt(ctx, d_add->dflts[0], (struct lysc_node_choice*)devs[u]->target)) {
Radek Krejciccd20f12019-02-15 14:12:27 +01006177 goto cleanup;
6178 }
6179 break;
6180 default:
6181 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006182 lys_nodetype2str(devs[u]->target->nodetype), "add", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01006183 goto cleanup;
6184 }
6185 }
6186
6187 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01006188 if (d_add->flags & LYS_CONFIG_MASK) {
6189 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci327de162019-06-14 12:52:07 +02006190 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci93dcc392019-02-19 10:43:38 +01006191 lys_nodetype2str(devs[u]->target->nodetype), "add", "config");
6192 goto cleanup;
6193 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006194 if (devs[u]->flags) {
Radek Krejci327de162019-06-14 12:52:07 +02006195 LOGWRN(ctx->ctx, "Deviating config inside %s has no effect.",
6196 devs[u]->flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006197 }
Radek Krejci93dcc392019-02-19 10:43:38 +01006198 if (devs[u]->target->flags & LYS_SET_CONFIG) {
6199 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006200 "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").",
6201 devs[u]->target->flags & LYS_CONFIG_W ? "true" : "false");
Radek Krejci93dcc392019-02-19 10:43:38 +01006202 goto cleanup;
6203 }
Radek Krejci327de162019-06-14 12:52:07 +02006204 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_add->flags, 0, 0), cleanup);
Radek Krejci93dcc392019-02-19 10:43:38 +01006205 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006206
6207 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01006208 if (d_add->flags & LYS_MAND_MASK) {
6209 if (devs[u]->target->flags & LYS_MAND_MASK) {
6210 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006211 "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").",
6212 devs[u]->target->flags & LYS_MAND_TRUE ? "true" : "false");
Radek Krejcif1421c22019-02-19 13:05:20 +01006213 goto cleanup;
6214 }
Radek Krejci327de162019-06-14 12:52:07 +02006215 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_add->flags, 0), cleanup);
Radek Krejcif1421c22019-02-19 13:05:20 +01006216 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006217
6218 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006219 if (d_add->flags & LYS_SET_MIN) {
6220 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6221 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
6222 /* change value */
6223 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_add->min;
6224 } else if (devs[u]->target->nodetype == LYS_LIST) {
6225 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
6226 /* change value */
6227 ((struct lysc_node_list*)devs[u]->target)->min = d_add->min;
6228 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006229 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006230 lys_nodetype2str(devs[u]->target->nodetype), "add", "min-elements");
6231 goto cleanup;
6232 }
6233 if (d_add->min) {
6234 devs[u]->target->flags |= LYS_MAND_TRUE;
6235 }
6236 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006237
6238 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006239 if (d_add->flags & LYS_SET_MAX) {
6240 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6241 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
6242 /* change value */
6243 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
6244 } else if (devs[u]->target->nodetype == LYS_LIST) {
6245 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
6246 /* change value */
6247 ((struct lysc_node_list*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
6248 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006249 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006250 lys_nodetype2str(devs[u]->target->nodetype), "add", "max-elements");
6251 goto cleanup;
6252 }
6253 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006254
6255 break;
6256 case LYS_DEV_DELETE:
6257 d_del = (struct lysp_deviate_del*)d;
6258
6259 /* [units-stmt] */
6260 if (d_del->units) {
6261 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units");
Radek Krejcia1911222019-07-22 17:24:50 +02006262 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, 0, units, "deleting", "units", d_del->units);
6263 if (strcmp(((struct lysc_node_leaf*)devs[u]->target)->units, d_del->units)) {
6264 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
6265 "Invalid deviation deleting \"units\" property \"%s\" which does not match the target's property value \"%s\".",
6266 d_del->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
6267 goto cleanup;
6268 }
6269 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
6270 ((struct lysc_node_leaf*)devs[u]->target)->units = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01006271 }
6272
6273 /* *must-stmt */
6274 if (d_del->musts) {
6275 switch (devs[u]->target->nodetype) {
6276 case LYS_CONTAINER:
6277 case LYS_LIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006278 DEV_DEL_ARRAY(struct lysc_node_container*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01006279 break;
6280 case LYS_LEAF:
6281 case LYS_LEAFLIST:
6282 case LYS_ANYDATA:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006283 DEV_DEL_ARRAY(struct lysc_node_leaf*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01006284 break;
6285 case LYS_NOTIF:
Radek Krejcifc11bd72019-04-11 16:00:05 +02006286 DEV_DEL_ARRAY(struct lysc_notif*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006287 break;
6288 case LYS_ACTION:
6289 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
6290 DEV_DEL_ARRAY(struct lysc_action*, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
6291 break;
6292 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
6293 DEV_DEL_ARRAY(struct lysc_action*, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
6294 break;
6295 }
6296 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01006297 default:
6298 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006299 lys_nodetype2str(devs[u]->target->nodetype), "delete", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01006300 goto cleanup;
6301 }
6302 }
6303
6304 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01006305 if (d_del->uniques) {
6306 DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique");
6307 list = (struct lysc_node_list*)devs[u]->target; /* shortcut */
6308 LY_ARRAY_FOR(d_del->uniques, x) {
6309 LY_ARRAY_FOR(list->uniques, z) {
6310 for (name = d_del->uniques[x], y = 0; name; name = nodeid, ++y) {
6311 nodeid = strpbrk(name, " \t\n");
6312 if (nodeid) {
Radek Krejci7f9b6512019-09-18 13:11:09 +02006313 if (ly_strncmp(list->uniques[z][y]->name, name, nodeid - name)) {
Radek Krejci7af64242019-02-18 13:07:53 +01006314 break;
6315 }
6316 while (isspace(*nodeid)) {
6317 ++nodeid;
6318 }
6319 } else {
6320 if (strcmp(name, list->uniques[z][y]->name)) {
6321 break;
6322 }
6323 }
6324 }
6325 if (!name) {
6326 /* complete match - remove the unique */
6327 LY_ARRAY_DECREMENT(list->uniques);
6328 LY_ARRAY_FREE(list->uniques[z]);
6329 memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_SIZE(list->uniques) - z) * (sizeof *list->uniques));
6330 --z;
6331 break;
6332 }
6333 }
6334 if (!list->uniques || z == LY_ARRAY_SIZE(list->uniques)) {
6335 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006336 "Invalid deviation deleting \"unique\" property \"%s\" which does not match any of the target's property values.",
6337 d_del->uniques[x]);
Radek Krejci7af64242019-02-18 13:07:53 +01006338 goto cleanup;
6339 }
6340 }
6341 if (!LY_ARRAY_SIZE(list->uniques)) {
6342 LY_ARRAY_FREE(list->uniques);
6343 list->uniques = NULL;
6344 }
6345 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006346
6347 /* *default-stmt */
6348 if (d_del->dflts) {
6349 switch (devs[u]->target->nodetype) {
6350 case LYS_LEAF:
6351 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
6352 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
6353 dflt, "deleting", "default", d_del->dflts[0]);
6354
Radek Krejcia1911222019-07-22 17:24:50 +02006355 /* check that the values matches */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006356 dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, lys_get_prefix, leaf->dflt_mod, &i);
Radek Krejcia1911222019-07-22 17:24:50 +02006357 if (strcmp(dflt, d_del->dflts[0])) {
6358 if (i) {
6359 free((char*)dflt);
6360 }
6361 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
6362 "Invalid deviation deleting \"default\" property \"%s\" which does not match the target's property value \"%s\".",
6363 d_del->dflts[0], dflt);
6364 goto cleanup;
6365 }
6366 if (i) {
6367 free((char*)dflt);
6368 }
6369 dflt = NULL;
6370
Radek Krejci474f9b82019-07-24 11:36:37 +02006371 /* update the list of incomplete default values if needed */
6372 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6373
Radek Krejcia1911222019-07-22 17:24:50 +02006374 /* remove the default specification */
6375 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6376 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6377 free(leaf->dflt);
6378 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006379 leaf->dflt_mod = NULL;
Radek Krejci551b12c2019-02-19 16:11:21 +01006380 devs[u]->target->flags &= ~LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01006381 break;
6382 case LYS_LEAFLIST:
Radek Krejcia1911222019-07-22 17:24:50 +02006383 DEV_CHECK_PRESENCE(struct lysc_node_leaflist*, 0, dflts, "deleting", "default", d_del->dflts[0]);
6384 LY_ARRAY_FOR(d_del->dflts, x) {
6385 LY_ARRAY_FOR(llist->dflts, y) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006386 dflt = llist->type->plugin->print(llist->dflts[y], LYD_XML, lys_get_prefix, llist->dflts_mods[y], &i);
Radek Krejcia1911222019-07-22 17:24:50 +02006387 if (!strcmp(dflt, d_del->dflts[x])) {
6388 if (i) {
6389 free((char*)dflt);
6390 }
6391 dflt = NULL;
6392 break;
6393 }
6394 if (i) {
6395 free((char*)dflt);
6396 }
6397 dflt = NULL;
6398 }
6399 if (y == LY_ARRAY_SIZE(llist->dflts)) {
6400 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid deviation deleting \"default\" property \"%s\" "
6401 "which does not match any of the target's property values.", d_del->dflts[x]);
6402 goto cleanup;
6403 }
Radek Krejci474f9b82019-07-24 11:36:37 +02006404
6405 /* update the list of incomplete default values if needed */
6406 lysc_incomplete_dflts_remove(ctx, llist->dflts[y]);
6407
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006408 LY_ARRAY_DECREMENT(llist->dflts_mods);
Radek Krejcia1911222019-07-22 17:24:50 +02006409 LY_ARRAY_DECREMENT(llist->dflts);
6410 llist->dflts[y]->realtype->plugin->free(ctx->ctx, llist->dflts[y]);
6411 lysc_type_free(ctx->ctx, llist->dflts[y]->realtype);
6412 free(llist->dflts[y]);
6413 memmove(&llist->dflts[y], &llist->dflts[y + 1], (LY_ARRAY_SIZE(llist->dflts) - y) * (sizeof *llist->dflts));
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006414 memmove(&llist->dflts_mods[y], &llist->dflts_mods[y + 1], (LY_ARRAY_SIZE(llist->dflts_mods) - y) * (sizeof *llist->dflts_mods));
Radek Krejcia1911222019-07-22 17:24:50 +02006415 }
6416 if (!LY_ARRAY_SIZE(llist->dflts)) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006417 LY_ARRAY_FREE(llist->dflts_mods);
6418 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006419 LY_ARRAY_FREE(llist->dflts);
6420 llist->dflts = NULL;
6421 llist->flags &= ~LYS_SET_DFLT;
Radek Krejci551b12c2019-02-19 16:11:21 +01006422 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006423 break;
6424 case LYS_CHOICE:
6425 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
6426 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "deleting", "default", d_del->dflts[0]);
6427 nodeid = d_del->dflts[0];
Radek Krejcib4a4a272019-06-10 12:44:52 +02006428 LY_CHECK_GOTO(ly_parse_nodeid(&nodeid, &prefix, &prefix_len, &name, &name_len), cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01006429 if (prefix) {
6430 /* use module prefixes from the deviation module to match the module of the default case */
6431 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
6432 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006433 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
6434 "The prefix does not match any imported module of the deviation module.", d_del->dflts[0]);
Radek Krejciccd20f12019-02-15 14:12:27 +01006435 goto cleanup;
6436 }
6437 if (mod != ((struct lysc_node_choice*)devs[u]->target)->dflt->module) {
6438 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006439 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
6440 "The prefix does not match the default case's module.", d_del->dflts[0]);
Radek Krejciccd20f12019-02-15 14:12:27 +01006441 goto cleanup;
6442 }
6443 }
6444 /* else {
6445 * strictly, the default prefix would point to the deviation module, but the value should actually
6446 * match the default string in the original module (usually unprefixed), so in this case we do not check
6447 * the module of the default case, just matching its name */
6448 if (strcmp(name, ((struct lysc_node_choice*)devs[u]->target)->dflt->name)) {
6449 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006450 "Invalid deviation deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".",
6451 d_del->dflts[0], ((struct lysc_node_choice*)devs[u]->target)->dflt->name);
Radek Krejciccd20f12019-02-15 14:12:27 +01006452 goto cleanup;
6453 }
6454 ((struct lysc_node_choice*)devs[u]->target)->dflt->flags &= ~LYS_SET_DFLT;
6455 ((struct lysc_node_choice*)devs[u]->target)->dflt = NULL;
6456 break;
6457 default:
6458 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006459 lys_nodetype2str(devs[u]->target->nodetype), "delete", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01006460 goto cleanup;
6461 }
6462 }
6463
6464 break;
6465 case LYS_DEV_REPLACE:
6466 d_rpl = (struct lysp_deviate_rpl*)d;
6467
6468 /* [type-stmt] */
Radek Krejci33f72892019-02-21 10:36:58 +01006469 if (d_rpl->type) {
6470 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type");
6471 /* type is mandatory, so checking for its presence is not necessary */
6472 lysc_type_free(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->type);
Radek Krejcia1911222019-07-22 17:24:50 +02006473
6474 if (leaf->dflt && !(devs[u]->target->flags & LYS_SET_DFLT)) {
6475 /* the target has default from the previous type - remove it */
6476 if (devs[u]->target->nodetype == LYS_LEAF) {
Radek Krejci474f9b82019-07-24 11:36:37 +02006477 /* update the list of incomplete default values if needed */
6478 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6479
Radek Krejcia1911222019-07-22 17:24:50 +02006480 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6481 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6482 free(leaf->dflt);
6483 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006484 leaf->dflt_mod = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006485 } else { /* LYS_LEAFLIST */
6486 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejci474f9b82019-07-24 11:36:37 +02006487 lysc_incomplete_dflts_remove(ctx, llist->dflts[x]);
Radek Krejcia1911222019-07-22 17:24:50 +02006488 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6489 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6490 free(llist->dflts[x]);
6491 }
6492 LY_ARRAY_FREE(llist->dflts);
6493 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006494 LY_ARRAY_FREE(llist->dflts_mods);
6495 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006496 }
6497 }
6498 if (!leaf->dflt) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006499 /* there is no default value, do not set changed_type after type compilation
6500 * which is used to recompile the default value */
Radek Krejcia1911222019-07-22 17:24:50 +02006501 changed_type = -1;
6502 }
Radek Krejciec4da802019-05-02 13:02:41 +02006503 LY_CHECK_GOTO(lys_compile_node_type(ctx, NULL, d_rpl->type, (struct lysc_node_leaf*)devs[u]->target), cleanup);
Radek Krejcia1911222019-07-22 17:24:50 +02006504 changed_type++;
Radek Krejci33f72892019-02-21 10:36:58 +01006505 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006506
6507 /* [units-stmt] */
6508 if (d_rpl->units) {
6509 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units");
6510 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_UNITS),
6511 units, "replacing", "units", d_rpl->units);
6512
6513 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
6514 DUP_STRING(ctx->ctx, d_rpl->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
6515 }
6516
6517 /* [default-stmt] */
6518 if (d_rpl->dflt) {
6519 switch (devs[u]->target->nodetype) {
6520 case LYS_LEAF:
6521 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
6522 dflt, "replacing", "default", d_rpl->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02006523 /* first, remove the default value taken from the type */
Radek Krejci474f9b82019-07-24 11:36:37 +02006524 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02006525 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6526 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6527 dflt = d_rpl->dflt;
6528 /* parsing is done at the end after possible replace of the leaf's type */
Radek Krejciccd20f12019-02-15 14:12:27 +01006529 break;
6530 case LYS_CHOICE:
6531 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "replacing", "default", d_rpl->dflt);
Radek Krejci327de162019-06-14 12:52:07 +02006532 if (lys_compile_deviation_set_choice_dflt(ctx, d_rpl->dflt, (struct lysc_node_choice*)devs[u]->target)) {
Radek Krejciccd20f12019-02-15 14:12:27 +01006533 goto cleanup;
6534 }
6535 break;
6536 default:
6537 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006538 lys_nodetype2str(devs[u]->target->nodetype), "replace", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01006539 goto cleanup;
6540 }
6541 }
6542
6543 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01006544 if (d_rpl->flags & LYS_CONFIG_MASK) {
6545 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci327de162019-06-14 12:52:07 +02006546 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci93dcc392019-02-19 10:43:38 +01006547 lys_nodetype2str(devs[u]->target->nodetype), "replace", "config");
6548 goto cleanup;
6549 }
6550 if (!(devs[u]->target->flags & LYS_SET_CONFIG)) {
Radek Krejci327de162019-06-14 12:52:07 +02006551 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
Radek Krejci93dcc392019-02-19 10:43:38 +01006552 "replacing", "config", d_rpl->flags & LYS_CONFIG_W ? "config true" : "config false");
6553 goto cleanup;
6554 }
Radek Krejci327de162019-06-14 12:52:07 +02006555 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_rpl->flags, 0, 0), cleanup);
Radek Krejci93dcc392019-02-19 10:43:38 +01006556 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006557
6558 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01006559 if (d_rpl->flags & LYS_MAND_MASK) {
6560 if (!(devs[u]->target->flags & LYS_MAND_MASK)) {
Radek Krejci327de162019-06-14 12:52:07 +02006561 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
Radek Krejcif1421c22019-02-19 13:05:20 +01006562 "replacing", "mandatory", d_rpl->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false");
6563 goto cleanup;
6564 }
Radek Krejci327de162019-06-14 12:52:07 +02006565 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_rpl->flags, 0), cleanup);
Radek Krejcif1421c22019-02-19 13:05:20 +01006566 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006567
6568 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006569 if (d_rpl->flags & LYS_SET_MIN) {
6570 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6571 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
6572 /* change value */
6573 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_rpl->min;
6574 } else if (devs[u]->target->nodetype == LYS_LIST) {
6575 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
6576 /* change value */
6577 ((struct lysc_node_list*)devs[u]->target)->min = d_rpl->min;
6578 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006579 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006580 lys_nodetype2str(devs[u]->target->nodetype), "replace", "min-elements");
6581 goto cleanup;
6582 }
6583 if (d_rpl->min) {
6584 devs[u]->target->flags |= LYS_MAND_TRUE;
6585 }
6586 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006587
6588 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006589 if (d_rpl->flags & LYS_SET_MAX) {
6590 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6591 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
6592 /* change value */
6593 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
6594 } else if (devs[u]->target->nodetype == LYS_LIST) {
6595 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
6596 /* change value */
6597 ((struct lysc_node_list*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
6598 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006599 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006600 lys_nodetype2str(devs[u]->target->nodetype), "replace", "max-elements");
6601 goto cleanup;
6602 }
6603 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006604
6605 break;
6606 default:
6607 LOGINT(ctx->ctx);
6608 goto cleanup;
6609 }
6610 }
Radek Krejci551b12c2019-02-19 16:11:21 +01006611
Radek Krejci33f72892019-02-21 10:36:58 +01006612 /* final check when all deviations of a single target node are applied */
6613
Radek Krejci551b12c2019-02-19 16:11:21 +01006614 /* check min-max compatibility */
6615 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6616 min = ((struct lysc_node_leaflist*)devs[u]->target)->min;
6617 max = ((struct lysc_node_leaflist*)devs[u]->target)->max;
6618 } else if (devs[u]->target->nodetype == LYS_LIST) {
6619 min = ((struct lysc_node_list*)devs[u]->target)->min;
6620 max = ((struct lysc_node_list*)devs[u]->target)->max;
6621 } else {
6622 min = max = 0;
6623 }
6624 if (min > max) {
6625 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid combination of min-elements and max-elements "
Radek Krejci327de162019-06-14 12:52:07 +02006626 "after deviation: min value %u is bigger than max value %u.", min, max);
Radek Krejci551b12c2019-02-19 16:11:21 +01006627 goto cleanup;
6628 }
6629
Radek Krejcia1911222019-07-22 17:24:50 +02006630 if (dflt) {
6631 /* parse added/changed default value after possible change of the type */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006632 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02006633 leaf->dflt->realtype = leaf->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006634 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt),
6635 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006636 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, devs[u]->target, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006637 leaf->dflt->realtype->refcount++;
6638 if (err) {
6639 ly_err_print(err);
6640 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6641 "Invalid deviation setting \"default\" property \"%s\" which does not fit the type (%s).", dflt, err->msg);
6642 ly_err_free(err);
6643 }
Radek Krejci474f9b82019-07-24 11:36:37 +02006644 if (rc == LY_EINCOMPLETE) {
6645 /* postpone default compilation when the tree is complete */
6646 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup);
6647
6648 /* but in general result is so far ok */
6649 rc = LY_SUCCESS;
6650 }
Radek Krejcia1911222019-07-22 17:24:50 +02006651 LY_CHECK_GOTO(rc, cleanup);
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006652 } else if (changed_type) {
Radek Krejcia1911222019-07-22 17:24:50 +02006653 /* the leaf/leaf-list's type has changed, but there is still a default value for the previous type */
6654 int dynamic;
6655 if (devs[u]->target->nodetype == LYS_LEAF) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006656 dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, lys_get_prefix, leaf->dflt_mod, &dynamic);
Radek Krejci474f9b82019-07-24 11:36:37 +02006657
6658 /* update the list of incomplete default values if needed */
6659 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6660
6661 /* remove the previous default */
Radek Krejcia1911222019-07-22 17:24:50 +02006662 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6663 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6664 leaf->dflt->realtype = leaf->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006665 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt),
6666 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006667 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, devs[u]->target, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006668 leaf->dflt->realtype->refcount++;
6669 if (err) {
6670 ly_err_print(err);
6671 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6672 "Invalid deviation replacing leaf's type - the leaf's default value \"%s\" does not match the type (%s).", dflt, err->msg);
6673 ly_err_free(err);
6674 }
6675 if (dynamic) {
6676 free((void*)dflt);
6677 }
Radek Krejcia1911222019-07-22 17:24:50 +02006678 dflt = NULL;
Radek Krejci474f9b82019-07-24 11:36:37 +02006679 if (rc == LY_EINCOMPLETE) {
6680 /* postpone default compilation when the tree is complete */
6681 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup);
6682
6683 /* but in general result is so far ok */
6684 rc = LY_SUCCESS;
6685 }
6686 LY_CHECK_GOTO(rc, cleanup);
Radek Krejcia1911222019-07-22 17:24:50 +02006687 } else { /* LYS_LEAFLIST */
6688 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006689 dflt = llist->dflts[x]->realtype->plugin->print(llist->dflts[x], LYD_XML, lys_get_prefix, llist->dflts_mods[x], &dynamic);
Radek Krejcia1911222019-07-22 17:24:50 +02006690 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6691 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6692 llist->dflts[x]->realtype = llist->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006693 rc = llist->type->plugin->store(ctx->ctx, llist->type, dflt, strlen(dflt),
6694 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006695 lys_resolve_prefix, (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL,
6696 llist->dflts[x], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006697 llist->dflts[x]->realtype->refcount++;
6698 if (err) {
6699 ly_err_print(err);
6700 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6701 "Invalid deviation replacing leaf-list's type - the leaf-list's default value \"%s\" does not match the type (%s).",
6702 dflt, err->msg);
6703 ly_err_free(err);
6704 }
6705 if (dynamic) {
6706 free((void*)dflt);
6707 }
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006708 dflt = NULL;
Radek Krejci474f9b82019-07-24 11:36:37 +02006709 if (rc == LY_EINCOMPLETE) {
6710 /* postpone default compilation when the tree is complete */
6711 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup);
6712
6713 /* but in general result is so far ok */
6714 rc = LY_SUCCESS;
6715 }
Radek Krejcia1911222019-07-22 17:24:50 +02006716 LY_CHECK_GOTO(rc, cleanup);
6717 }
6718 }
6719 }
6720
Radek Krejci551b12c2019-02-19 16:11:21 +01006721 /* check mandatory - default compatibility */
6722 if ((devs[u]->target->nodetype & (LYS_LEAF | LYS_LEAFLIST))
6723 && (devs[u]->target->flags & LYS_SET_DFLT)
6724 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
6725 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02006726 "Invalid deviation combining default value and mandatory %s.", lys_nodetype2str(devs[u]->target->nodetype));
Radek Krejci551b12c2019-02-19 16:11:21 +01006727 goto cleanup;
6728 } else if ((devs[u]->target->nodetype & LYS_CHOICE)
6729 && ((struct lysc_node_choice*)devs[u]->target)->dflt
6730 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
Radek Krejci327de162019-06-14 12:52:07 +02006731 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation combining default case and mandatory choice.");
Radek Krejci551b12c2019-02-19 16:11:21 +01006732 goto cleanup;
6733 }
6734 if (devs[u]->target->parent && (devs[u]->target->parent->flags & LYS_SET_DFLT) && (devs[u]->target->flags & LYS_MAND_TRUE)) {
6735 /* mandatory node under a default case */
6736 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02006737 "Invalid deviation combining mandatory %s \"%s\" in a default choice's case \"%s\".",
6738 lys_nodetype2str(devs[u]->target->nodetype), devs[u]->target->name, devs[u]->target->parent->name);
Radek Krejci551b12c2019-02-19 16:11:21 +01006739 goto cleanup;
6740 }
Radek Krejci33f72892019-02-21 10:36:58 +01006741
Radek Krejci327de162019-06-14 12:52:07 +02006742 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006743 }
6744
Radek Krejci327de162019-06-14 12:52:07 +02006745 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006746 ret = LY_SUCCESS;
6747
6748cleanup:
6749 for (u = 0; u < devs_p.count && devs[u]; ++u) {
6750 LY_ARRAY_FREE(devs[u]->deviates);
6751 free(devs[u]);
6752 }
6753 free(devs);
6754 ly_set_erase(&targets, NULL);
6755 ly_set_erase(&devs_p, NULL);
6756
6757 return ret;
6758}
6759
Radek Krejcib56c7502019-02-13 14:19:54 +01006760/**
Radek Krejcid05cbd92018-12-05 14:26:40 +01006761 * @brief Compile the given YANG submodule into the main module.
6762 * @param[in] ctx Compile context
6763 * @param[in] inc Include structure from the main module defining the submodule.
Radek Krejcid05cbd92018-12-05 14:26:40 +01006764 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
6765 */
6766LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02006767lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc)
Radek Krejcid05cbd92018-12-05 14:26:40 +01006768{
6769 unsigned int u;
6770 LY_ERR ret = LY_SUCCESS;
6771 /* shortcuts */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006772 struct lysp_submodule *submod = inc->submodule;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006773 struct lysc_module *mainmod = ctx->mod->compiled;
Radek Krejci474f9b82019-07-24 11:36:37 +02006774 struct lysp_node *node_p;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006775
Radek Krejci0af46292019-01-11 16:02:31 +01006776 if (!mainmod->mod->off_features) {
6777 /* features are compiled directly into the compiled module structure,
6778 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves.
6779 * The features compilation is finished in the main module (lys_compile()). */
Radek Krejci0935f412019-08-20 16:15:18 +02006780 ret = lys_feature_precompile(ctx, NULL, NULL, submod->features, &mainmod->features);
6781 LY_CHECK_GOTO(ret, error);
6782 }
6783 if (!mainmod->mod->off_extensions) {
6784 /* extensions are compiled directly into the compiled module structure, compilation is finished in the main module (lys_compile()). */
6785 ret = lys_extension_precompile(ctx, NULL, NULL, submod->extensions, &mainmod->extensions);
Radek Krejci0af46292019-01-11 16:02:31 +01006786 LY_CHECK_GOTO(ret, error);
6787 }
6788
Radek Krejci327de162019-06-14 12:52:07 +02006789 lysc_update_path(ctx, NULL, "{identity}");
Radek Krejciec4da802019-05-02 13:02:41 +02006790 COMPILE_ARRAY_UNIQUE_GOTO(ctx, submod->identities, mainmod->identities, u, lys_compile_identity, ret, error);
Radek Krejci327de162019-06-14 12:52:07 +02006791 lysc_update_path(ctx, NULL, NULL);
Radek Krejcid05cbd92018-12-05 14:26:40 +01006792
Radek Krejci474f9b82019-07-24 11:36:37 +02006793 /* data nodes */
6794 LY_LIST_FOR(submod->data, node_p) {
6795 ret = lys_compile_node(ctx, node_p, NULL, 0);
6796 LY_CHECK_GOTO(ret, error);
6797 }
Radek Krejci8cce8532019-03-05 11:27:45 +01006798
Radek Krejciec4da802019-05-02 13:02:41 +02006799 COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, u, lys_compile_action, 0, ret, error);
6800 COMPILE_ARRAY1_GOTO(ctx, submod->notifs, mainmod->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejci8cce8532019-03-05 11:27:45 +01006801
Radek Krejcid05cbd92018-12-05 14:26:40 +01006802error:
6803 return ret;
6804}
6805
Radek Krejci335332a2019-09-05 13:03:35 +02006806static void *
6807lys_compile_extension_instance_storage(enum ly_stmt stmt, struct lysc_ext_substmt *substmts)
6808{
6809 for (unsigned int u = 0; substmts[u].stmt; ++u) {
6810 if (substmts[u].stmt == stmt) {
6811 return substmts[u].storage;
6812 }
6813 }
6814 return NULL;
6815}
6816
6817LY_ERR
6818lys_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *ext, struct lysc_ext_substmt *substmts)
6819{
6820 LY_ERR ret = LY_EVALID, r;
6821 unsigned int u;
6822 struct lysp_stmt *stmt;
6823 void *parsed = NULL, **compiled = NULL;
Radek Krejci335332a2019-09-05 13:03:35 +02006824
6825 /* check for invalid substatements */
6826 for (stmt = ext->child; stmt; stmt = stmt->next) {
Radek Krejcif56e2a42019-09-09 14:15:25 +02006827 if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) {
6828 continue;
6829 }
Radek Krejci335332a2019-09-05 13:03:35 +02006830 for (u = 0; substmts[u].stmt; ++u) {
6831 if (substmts[u].stmt == stmt->kw) {
6832 break;
6833 }
6834 }
6835 if (!substmts[u].stmt) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006836 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\" as a child of \"%s%s%s\" extension instance.",
6837 stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
Radek Krejci335332a2019-09-05 13:03:35 +02006838 goto cleanup;
6839 }
Radek Krejci335332a2019-09-05 13:03:35 +02006840 }
6841
Radek Krejciad5963b2019-09-06 16:03:05 +02006842 /* TODO store inherited data, e.g. status first, but mark them somehow to allow to overwrite them and not detect duplicity */
6843
Radek Krejci335332a2019-09-05 13:03:35 +02006844 /* keep order of the processing the same as the order in the defined substmts,
6845 * the order is important for some of the statements depending on others (e.g. type needs status and units) */
6846 for (u = 0; substmts[u].stmt; ++u) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006847 int stmt_present = 0;
6848
Radek Krejci335332a2019-09-05 13:03:35 +02006849 for (stmt = ext->child; stmt; stmt = stmt->next) {
6850 if (substmts[u].stmt != stmt->kw) {
6851 continue;
6852 }
6853
Radek Krejciad5963b2019-09-06 16:03:05 +02006854 stmt_present = 1;
Radek Krejci335332a2019-09-05 13:03:35 +02006855 if (substmts[u].storage) {
6856 switch (stmt->kw) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006857 case LY_STMT_STATUS:
6858 assert(substmts[u].cardinality < LY_STMT_CARD_SOME);
6859 LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &substmts[u].storage, /* TODO */ NULL), ret = r, cleanup);
6860 break;
6861 case LY_STMT_UNITS: {
6862 const char **units;
6863
6864 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6865 /* single item */
6866 if (*((const char **)substmts[u].storage)) {
6867 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6868 goto cleanup;
6869 }
6870 units = (const char **)substmts[u].storage;
6871 } else {
6872 /* sized array */
6873 const char ***units_array = (const char ***)substmts[u].storage;
6874 LY_ARRAY_NEW_GOTO(ctx->ctx, *units_array, units, ret, cleanup);
6875 }
6876 *units = lydict_insert(ctx->ctx, stmt->arg, 0);
6877 break;
6878 }
Radek Krejci335332a2019-09-05 13:03:35 +02006879 case LY_STMT_TYPE: {
6880 uint16_t *flags = lys_compile_extension_instance_storage(LY_STMT_STATUS, substmts);
6881 const char **units = lys_compile_extension_instance_storage(LY_STMT_UNITS, substmts);
6882
6883 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6884 /* single item */
Radek Krejciad5963b2019-09-06 16:03:05 +02006885 if (*(struct lysc_type**)substmts[u].storage) {
Radek Krejci335332a2019-09-05 13:03:35 +02006886 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6887 goto cleanup;
6888 }
6889 compiled = substmts[u].storage;
6890 } else {
6891 /* sized array */
6892 struct lysc_type ***types = (struct lysc_type***)substmts[u].storage, **type = NULL;
6893 LY_ARRAY_NEW_GOTO(ctx->ctx, *types, type, ret, cleanup);
6894 compiled = (void*)type;
6895 }
6896
Radek Krejciad5963b2019-09-06 16:03:05 +02006897 LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &parsed, NULL), ret = r, cleanup);
Radek Krejci335332a2019-09-05 13:03:35 +02006898 LY_CHECK_ERR_GOTO(r = lys_compile_type(ctx, ext->parent_type == LYEXT_PAR_NODE ? ((struct lysc_node*)ext->parent)->sp : NULL,
6899 flags ? *flags : 0, ctx->mod_def->parsed, ext->name, parsed, (struct lysc_type**)compiled,
Radek Krejci38d85362019-09-05 16:26:38 +02006900 units && !*units ? units : NULL), lysp_type_free(ctx->ctx, parsed); free(parsed); ret = r, cleanup);
6901 lysp_type_free(ctx->ctx, parsed);
6902 free(parsed);
Radek Krejci335332a2019-09-05 13:03:35 +02006903 break;
6904 }
Radek Krejciad5963b2019-09-06 16:03:05 +02006905 case LY_STMT_IF_FEATURE: {
6906 struct lysc_iffeature *iff = NULL;
6907
6908 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6909 /* single item */
6910 if (((struct lysc_iffeature*)substmts[u].storage)->features) {
6911 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6912 goto cleanup;
6913 }
6914 iff = (struct lysc_iffeature*)substmts[u].storage;
6915 } else {
6916 /* sized array */
6917 struct lysc_iffeature **iffs = (struct lysc_iffeature**)substmts[u].storage;
6918 LY_ARRAY_NEW_GOTO(ctx->ctx, *iffs, iff, ret, cleanup);
6919 }
6920 LY_CHECK_ERR_GOTO(r = lys_compile_iffeature(ctx, &stmt->arg, iff), ret = r, cleanup);
6921 break;
6922 }
6923 /* TODO support other substatements (parse stmt to lysp and then compile lysp to lysc),
6924 * also note that in many statements their extensions are not taken into account */
Radek Krejci335332a2019-09-05 13:03:35 +02006925 default:
Radek Krejciad5963b2019-09-06 16:03:05 +02006926 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Statement \"%s\" is not supported as an extension (found in \"%s%s%s\") substatement.",
6927 stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
Radek Krejci335332a2019-09-05 13:03:35 +02006928 goto cleanup;
6929 }
6930 }
Radek Krejci335332a2019-09-05 13:03:35 +02006931 }
Radek Krejci335332a2019-09-05 13:03:35 +02006932
Radek Krejciad5963b2019-09-06 16:03:05 +02006933 if ((substmts[u].cardinality == LY_STMT_CARD_MAND || substmts[u].cardinality == LY_STMT_CARD_SOME) && !stmt_present) {
6934 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Missing mandatory keyword \"%s\" as a child of \"%s%s%s\".",
6935 ly_stmt2str(substmts[u].stmt), ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
6936 goto cleanup;
6937 }
Radek Krejci335332a2019-09-05 13:03:35 +02006938 }
6939
6940 ret = LY_SUCCESS;
6941
6942cleanup:
Radek Krejci335332a2019-09-05 13:03:35 +02006943 return ret;
6944}
6945
Michal Vasko175012e2019-11-06 15:49:14 +01006946/**
Michal Vaskoecd62de2019-11-13 12:35:11 +01006947 * @brief Check when for cyclic dependencies.
6948 * @param[in] set Set with all the referenced nodes.
6949 * @param[in] node Node whose "when" referenced nodes are in @p set.
6950 * @return LY_ERR value
6951 */
6952static LY_ERR
Michal Vasko5c4e5892019-11-14 12:31:38 +01006953lys_compile_check_when_cyclic(struct lyxp_set *set, const struct lysc_node *node)
Michal Vaskoecd62de2019-11-13 12:35:11 +01006954{
6955 struct lyxp_set tmp_set;
6956 struct lyxp_set_scnode *xp_scnode;
6957 uint32_t i, j;
6958 int idx;
6959 struct lysc_when *when;
6960 LY_ERR ret = LY_SUCCESS;
6961
6962 memset(&tmp_set, 0, sizeof tmp_set);
6963
6964 /* prepare in_ctx of the set */
6965 for (i = 0; i < set->used; ++i) {
6966 xp_scnode = &set->val.scnodes[i];
6967
Michal Vasko5c4e5892019-11-14 12:31:38 +01006968 if (xp_scnode->in_ctx != -1) {
6969 /* check node when, skip the context node (it was just checked) */
Michal Vaskoecd62de2019-11-13 12:35:11 +01006970 xp_scnode->in_ctx = 1;
6971 }
6972 }
6973
6974 for (i = 0; i < set->used; ++i) {
6975 xp_scnode = &set->val.scnodes[i];
6976 if (xp_scnode->in_ctx != 1) {
6977 /* already checked */
6978 continue;
6979 }
6980
6981 if ((xp_scnode->type != LYXP_NODE_ELEM) || !xp_scnode->scnode->when) {
6982 /* no when to check */
6983 xp_scnode->in_ctx = 0;
6984 continue;
6985 }
6986
6987 node = xp_scnode->scnode;
6988 do {
6989 LY_ARRAY_FOR(node->when, i) {
6990 when = node->when[i];
6991 ret = lyxp_atomize(when->cond, LYD_UNKNOWN, when->module, when->context,
6992 when->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, LYXP_SCNODE_SCHEMA);
6993 if (ret != LY_SUCCESS) {
6994 LOGVAL(set->ctx, LY_VLOG_LYS, node, LYVE_SEMANTICS, "Invalid when condition \"%s\".", when->cond->expr);
6995 goto cleanup;
6996 }
6997
6998 for (j = 0; j < tmp_set.used; ++j) {
6999 /* skip roots'n'stuff */
7000 if (tmp_set.val.scnodes[j].type == LYXP_NODE_ELEM) {
7001 /* try to find this node in our set */
7002 idx = lyxp_set_scnode_dup_node_check(set, tmp_set.val.scnodes[j].scnode, LYXP_NODE_ELEM, -1);
Michal Vasko5c4e5892019-11-14 12:31:38 +01007003 if ((idx > -1) && (set->val.scnodes[idx].in_ctx == -1)) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01007004 LOGVAL(set->ctx, LY_VLOG_LYS, node, LY_VCODE_CIRC_WHEN, node->name, set->val.scnodes[idx].scnode->name);
7005 ret = LY_EVALID;
7006 goto cleanup;
7007 }
7008
7009 /* needs to be checked, if in both sets, will be ignored */
7010 tmp_set.val.scnodes[j].in_ctx = 1;
7011 } else {
7012 /* no when, nothing to check */
7013 tmp_set.val.scnodes[j].in_ctx = 0;
7014 }
7015 }
7016
7017 /* merge this set into the global when set */
7018 lyxp_set_scnode_merge(set, &tmp_set);
7019 }
7020
7021 /* check when of non-data parents as well */
7022 node = node->parent;
7023 } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE)));
7024
7025 /* this node when was checked */
Michal Vasko5c4e5892019-11-14 12:31:38 +01007026 xp_scnode->in_ctx = -1;
Michal Vaskoecd62de2019-11-13 12:35:11 +01007027 }
7028
7029cleanup:
7030 lyxp_set_cast(&tmp_set, LYXP_SET_EMPTY);
7031 return ret;
7032}
7033
7034/**
Michal Vasko175012e2019-11-06 15:49:14 +01007035 * @brief Check when/must expressions of a node on a compiled schema tree.
7036 * @param[in] ctx Compile context.
7037 * @param[in] node Node to check.
7038 * @return LY_ERR value
7039 */
7040static LY_ERR
7041lys_compile_check_xpath(struct lysc_ctx *ctx, const struct lysc_node *node)
7042{
Michal Vasko175012e2019-11-06 15:49:14 +01007043 struct lyxp_set tmp_set;
7044 uint32_t i, j;
Michal Vasko5d8756a2019-11-07 15:21:00 +01007045 int opts, input_done = 0;
Michal Vasko175012e2019-11-06 15:49:14 +01007046 struct lysc_when **when = NULL;
7047 struct lysc_must *musts = NULL;
7048 LY_ERR ret = LY_SUCCESS;
7049
7050 memset(&tmp_set, 0, sizeof tmp_set);
Michal Vasko5d8756a2019-11-07 15:21:00 +01007051 opts = LYXP_SCNODE_SCHEMA;
Michal Vasko175012e2019-11-06 15:49:14 +01007052
7053 switch (node->nodetype) {
7054 case LYS_CONTAINER:
7055 when = ((struct lysc_node_container *)node)->when;
7056 musts = ((struct lysc_node_container *)node)->musts;
7057 break;
7058 case LYS_CHOICE:
7059 when = ((struct lysc_node_choice *)node)->when;
7060 break;
7061 case LYS_LEAF:
7062 when = ((struct lysc_node_leaf *)node)->when;
7063 musts = ((struct lysc_node_leaf *)node)->musts;
7064 break;
7065 case LYS_LEAFLIST:
7066 when = ((struct lysc_node_leaflist *)node)->when;
7067 musts = ((struct lysc_node_leaflist *)node)->musts;
7068 break;
7069 case LYS_LIST:
7070 when = ((struct lysc_node_list *)node)->when;
7071 musts = ((struct lysc_node_list *)node)->musts;
7072 break;
7073 case LYS_ANYXML:
7074 case LYS_ANYDATA:
7075 when = ((struct lysc_node_anydata *)node)->when;
7076 musts = ((struct lysc_node_anydata *)node)->musts;
7077 break;
7078 case LYS_CASE:
7079 when = ((struct lysc_node_case *)node)->when;
7080 break;
7081 case LYS_NOTIF:
7082 musts = ((struct lysc_notif *)node)->musts;
7083 break;
Michal Vasko5d8756a2019-11-07 15:21:00 +01007084 case LYS_ACTION:
7085 /* first process input musts */
7086 musts = ((struct lysc_action *)node)->input.musts;
7087 break;
Michal Vasko175012e2019-11-06 15:49:14 +01007088 default:
7089 /* nothing to check */
7090 break;
7091 }
7092
Michal Vasko175012e2019-11-06 15:49:14 +01007093 /* check "when" */
7094 LY_ARRAY_FOR(when, i) {
Michal Vasko175012e2019-11-06 15:49:14 +01007095 ret = lyxp_atomize(when[i]->cond, LYD_UNKNOWN, when[i]->module, when[i]->context,
7096 when[i]->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, opts);
7097 if (ret != LY_SUCCESS) {
7098 LOGVAL(ctx->ctx, LY_VLOG_LYS, node, LYVE_SEMANTICS, "Invalid when condition \"%s\".", when[i]->cond->expr);
7099 goto cleanup;
7100 }
7101
Michal Vaskodc052f32019-11-07 11:11:38 +01007102 ctx->path[0] = '\0';
7103 lysc_path((struct lysc_node *)node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
Michal Vasko175012e2019-11-06 15:49:14 +01007104 for (j = 0; j < tmp_set.used; ++j) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01007105 /* skip roots'n'stuff */
7106 if ((tmp_set.val.scnodes[j].type == LYXP_NODE_ELEM) && (tmp_set.val.scnodes[j].in_ctx != -1)) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01007107 struct lysc_node *schema = tmp_set.val.scnodes[j].scnode;
Michal Vasko175012e2019-11-06 15:49:14 +01007108
Michal Vaskoecd62de2019-11-13 12:35:11 +01007109 /* XPath expression cannot reference "lower" status than the node that has the definition */
7110 ret = lysc_check_status(ctx, when[i]->flags, when[i]->module, node->name, schema->flags, schema->module,
7111 schema->name);
7112 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko5c4e5892019-11-14 12:31:38 +01007113
7114 /* check dummy node accessing */
7115 if (schema == node) {
7116 LOGVAL(ctx->ctx, LY_VLOG_LYS, node, LY_VCODE_DUMMY_WHEN, node->name);
7117 ret = LY_EVALID;
7118 goto cleanup;
7119 }
Michal Vasko175012e2019-11-06 15:49:14 +01007120 }
7121 }
7122
Michal Vaskoecd62de2019-11-13 12:35:11 +01007123 /* check cyclic dependencies */
Michal Vasko5c4e5892019-11-14 12:31:38 +01007124 ret = lys_compile_check_when_cyclic(&tmp_set, node);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007125 LY_CHECK_GOTO(ret, cleanup);
7126
Michal Vasko175012e2019-11-06 15:49:14 +01007127 lyxp_set_cast(&tmp_set, LYXP_SET_EMPTY);
7128 }
7129
Michal Vasko5d8756a2019-11-07 15:21:00 +01007130check_musts:
Michal Vasko175012e2019-11-06 15:49:14 +01007131 /* check "must" */
7132 LY_ARRAY_FOR(musts, i) {
Michal Vasko175012e2019-11-06 15:49:14 +01007133 ret = lyxp_atomize(musts[i].cond, LYD_UNKNOWN, musts[i].module, node, LYXP_NODE_ELEM, &tmp_set, opts);
7134 if (ret != LY_SUCCESS) {
7135 LOGVAL(ctx->ctx, LY_VLOG_LYS, node, LYVE_SEMANTICS, "Invalid must restriction \"%s\".", musts[i].cond->expr);
7136 goto cleanup;
7137 }
7138
Michal Vaskodc052f32019-11-07 11:11:38 +01007139 ctx->path[0] = '\0';
7140 lysc_path((struct lysc_node *)node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
Michal Vasko175012e2019-11-06 15:49:14 +01007141 for (j = 0; j < tmp_set.used; ++j) {
7142 /* skip roots'n'stuff */
7143 if (tmp_set.val.scnodes[j].type == LYXP_NODE_ELEM) {
7144 /* XPath expression cannot reference "lower" status than the node that has the definition */
Michal Vasko5d8756a2019-11-07 15:21:00 +01007145 ret = lysc_check_status(ctx, node->flags, musts[i].module, node->name, tmp_set.val.scnodes[j].scnode->flags,
Michal Vasko175012e2019-11-06 15:49:14 +01007146 tmp_set.val.scnodes[j].scnode->module, tmp_set.val.scnodes[j].scnode->name);
7147 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko175012e2019-11-06 15:49:14 +01007148 }
7149 }
7150
7151 lyxp_set_cast(&tmp_set, LYXP_SET_EMPTY);
7152 }
7153
Michal Vasko5d8756a2019-11-07 15:21:00 +01007154 if ((node->nodetype == LYS_ACTION) && !input_done) {
7155 /* now check output musts */
7156 input_done = 1;
7157 musts = ((struct lysc_action *)node)->output.musts;
7158 opts = LYXP_SCNODE_OUTPUT;
7159 goto check_musts;
7160 }
7161
Michal Vasko175012e2019-11-06 15:49:14 +01007162cleanup:
7163 lyxp_set_cast(&tmp_set, LYXP_SET_EMPTY);
7164 return ret;
7165}
7166
Radek Krejci19a96102018-11-15 13:38:09 +01007167LY_ERR
7168lys_compile(struct lys_module *mod, int options)
7169{
7170 struct lysc_ctx ctx = {0};
7171 struct lysc_module *mod_c;
Radek Krejci412ddfa2018-11-23 11:44:11 +01007172 struct lysc_type *type, *typeiter;
Radek Krejci19a96102018-11-15 13:38:09 +01007173 struct lysp_module *sp;
7174 struct lysp_node *node_p;
Radek Krejci95710c92019-02-11 15:49:55 +01007175 struct lysp_augment **augments = NULL;
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007176 struct lysp_grp *grps;
Radek Krejci95710c92019-02-11 15:49:55 +01007177 struct lys_module *m;
Radek Krejcicdfecd92018-11-26 11:27:32 +01007178 unsigned int u, v;
Radek Krejcid05cbd92018-12-05 14:26:40 +01007179 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01007180
Radek Krejci0bcdaed2019-01-10 10:21:34 +01007181 LY_CHECK_ARG_RET(NULL, mod, mod->parsed, mod->ctx, LY_EINVAL);
Radek Krejci096235c2019-01-11 11:12:19 +01007182
7183 if (!mod->implemented) {
7184 /* just imported modules are not compiled */
7185 return LY_SUCCESS;
7186 }
7187
Radek Krejci19a96102018-11-15 13:38:09 +01007188 sp = mod->parsed;
7189
Radek Krejci0bcdaed2019-01-10 10:21:34 +01007190 ctx.ctx = mod->ctx;
Radek Krejci19a96102018-11-15 13:38:09 +01007191 ctx.mod = mod;
Radek Krejcie86bf772018-12-14 11:39:53 +01007192 ctx.mod_def = mod;
Radek Krejciec4da802019-05-02 13:02:41 +02007193 ctx.options = options;
Radek Krejci327de162019-06-14 12:52:07 +02007194 ctx.path_len = 1;
7195 ctx.path[0] = '/';
Radek Krejci19a96102018-11-15 13:38:09 +01007196
7197 mod->compiled = mod_c = calloc(1, sizeof *mod_c);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01007198 LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM);
7199 mod_c->mod = mod;
Radek Krejci19a96102018-11-15 13:38:09 +01007200
Radek Krejciec4da802019-05-02 13:02:41 +02007201 COMPILE_ARRAY_GOTO(&ctx, sp->imports, mod_c->imports, u, lys_compile_import, ret, error);
Radek Krejcid05cbd92018-12-05 14:26:40 +01007202 LY_ARRAY_FOR(sp->includes, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02007203 ret = lys_compile_submodule(&ctx, &sp->includes[u]);
Radek Krejcid05cbd92018-12-05 14:26:40 +01007204 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
7205 }
Radek Krejci0935f412019-08-20 16:15:18 +02007206
7207 /* features */
Radek Krejci0af46292019-01-11 16:02:31 +01007208 if (mod->off_features) {
7209 /* there is already precompiled array of features */
7210 mod_c->features = mod->off_features;
7211 mod->off_features = NULL;
Radek Krejci0af46292019-01-11 16:02:31 +01007212 } else {
7213 /* features are compiled directly into the compiled module structure,
7214 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves */
Radek Krejci327de162019-06-14 12:52:07 +02007215 ret = lys_feature_precompile(&ctx, NULL, NULL, sp->features, &mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01007216 LY_CHECK_GOTO(ret, error);
7217 }
7218 /* finish feature compilation, not only for the main module, but also for the submodules.
7219 * Due to possible forward references, it must be done when all the features (including submodules)
7220 * are present. */
7221 LY_ARRAY_FOR(sp->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02007222 ret = lys_feature_precompile_finish(&ctx, &sp->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01007223 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
7224 }
Radek Krejci327de162019-06-14 12:52:07 +02007225 lysc_update_path(&ctx, NULL, "{submodule}");
Radek Krejci0af46292019-01-11 16:02:31 +01007226 LY_ARRAY_FOR(sp->includes, v) {
Radek Krejci327de162019-06-14 12:52:07 +02007227 lysc_update_path(&ctx, NULL, sp->includes[v].name);
Radek Krejci0af46292019-01-11 16:02:31 +01007228 LY_ARRAY_FOR(sp->includes[v].submodule->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02007229 ret = lys_feature_precompile_finish(&ctx, &sp->includes[v].submodule->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01007230 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
7231 }
Radek Krejci327de162019-06-14 12:52:07 +02007232 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01007233 }
Radek Krejci327de162019-06-14 12:52:07 +02007234 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01007235
Radek Krejci0935f412019-08-20 16:15:18 +02007236 /* extensions */
7237 /* 2-steps: a) prepare compiled structures and ... */
7238 if (mod->off_extensions) {
7239 /* there is already precompiled array of extension definitions */
7240 mod_c->extensions = mod->off_extensions;
7241 mod->off_extensions = NULL;
7242 } else {
7243 /* extension definitions are compiled directly into the compiled module structure */
7244 ret = lys_extension_precompile(&ctx, NULL, NULL, sp->extensions, &mod_c->extensions);
Radek Krejcibfb2e142019-09-09 14:47:44 +02007245 LY_CHECK_GOTO(ret, error);
Radek Krejci0935f412019-08-20 16:15:18 +02007246 }
7247 /* ... b) connect the extension definitions with the appropriate extension plugins */
7248 lys_compile_extension_plugins(mod_c->extensions);
7249
7250 /* identities */
Radek Krejci327de162019-06-14 12:52:07 +02007251 lysc_update_path(&ctx, NULL, "{identity}");
Radek Krejciec4da802019-05-02 13:02:41 +02007252 COMPILE_ARRAY_UNIQUE_GOTO(&ctx, sp->identities, mod_c->identities, u, lys_compile_identity, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01007253 if (sp->identities) {
7254 LY_CHECK_RET(lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities));
7255 }
Radek Krejci327de162019-06-14 12:52:07 +02007256 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01007257
Radek Krejci95710c92019-02-11 15:49:55 +01007258 /* data nodes */
Radek Krejci19a96102018-11-15 13:38:09 +01007259 LY_LIST_FOR(sp->data, node_p) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007260 LY_CHECK_GOTO(ret = lys_compile_node(&ctx, node_p, NULL, 0), error);
Radek Krejci19a96102018-11-15 13:38:09 +01007261 }
Radek Krejci95710c92019-02-11 15:49:55 +01007262
Radek Krejciec4da802019-05-02 13:02:41 +02007263 COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error);
7264 COMPILE_ARRAY1_GOTO(&ctx, sp->notifs, mod_c->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejciccd20f12019-02-15 14:12:27 +01007265
Radek Krejci95710c92019-02-11 15:49:55 +01007266 /* augments - sort first to cover augments augmenting other augments */
Radek Krejcid3acfce2019-09-09 14:48:50 +02007267 LY_CHECK_GOTO(ret = lys_compile_augment_sort(&ctx, sp->augments, sp->includes, &augments), error);
Radek Krejci95710c92019-02-11 15:49:55 +01007268 LY_ARRAY_FOR(augments, u) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007269 LY_CHECK_GOTO(ret = lys_compile_augment(&ctx, augments[u], NULL), error);
Radek Krejci95710c92019-02-11 15:49:55 +01007270 }
Radek Krejciccd20f12019-02-15 14:12:27 +01007271
Radek Krejci474f9b82019-07-24 11:36:37 +02007272 /* deviations TODO cover deviations from submodules */
Radek Krejcid3acfce2019-09-09 14:48:50 +02007273 LY_CHECK_GOTO(ret = lys_compile_deviations(&ctx, sp), error);
Radek Krejci19a96102018-11-15 13:38:09 +01007274
Radek Krejci0935f412019-08-20 16:15:18 +02007275 /* extension instances TODO cover extension instances from submodules */
7276 COMPILE_EXTS_GOTO(&ctx, sp->exts, mod_c->exts, mod_c, LYEXT_PAR_MODULE, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01007277
Radek Krejcia3045382018-11-22 14:30:31 +01007278 /* validate leafref's paths and when/must xpaths */
Radek Krejci412ddfa2018-11-23 11:44:11 +01007279 /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which
7280 * can be also leafref, in case it is already resolved, go through the chain and check that it does not
7281 * 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 +01007282 for (u = 0; u < ctx.unres.count; ++u) {
Radek Krejci0e5d8382018-11-28 16:37:53 +01007283 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Radek Krejcia3045382018-11-22 14:30:31 +01007284 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
7285 if (type->basetype == LY_TYPE_LEAFREF) {
7286 /* validate the path */
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02007287 LY_CHECK_GOTO(ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[u]), (struct lysc_type_leafref*)type, NULL), error);
Radek Krejcicdfecd92018-11-26 11:27:32 +01007288 } else if (type->basetype == LY_TYPE_UNION) {
7289 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
7290 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
7291 /* validate the path */
Michal Vasko175012e2019-11-06 15:49:14 +01007292 ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[u]),
7293 (struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v], NULL);
7294 LY_CHECK_GOTO(ret, error);
Radek Krejcicdfecd92018-11-26 11:27:32 +01007295 }
7296 }
Radek Krejcia3045382018-11-22 14:30:31 +01007297 }
7298 }
Michal Vasko175012e2019-11-06 15:49:14 +01007299
7300 /* check xpath */
7301 LY_CHECK_GOTO(ret = lys_compile_check_xpath(&ctx, ctx.unres.objs[u]), error);
Radek Krejcia3045382018-11-22 14:30:31 +01007302 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01007303 for (u = 0; u < ctx.unres.count; ++u) {
Radek Krejci0e5d8382018-11-28 16:37:53 +01007304 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Radek Krejci412ddfa2018-11-23 11:44:11 +01007305 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
7306 if (type->basetype == LY_TYPE_LEAFREF) {
7307 /* store pointer to the real type */
7308 for (typeiter = ((struct lysc_type_leafref*)type)->realtype;
7309 typeiter->basetype == LY_TYPE_LEAFREF;
7310 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
7311 ((struct lysc_type_leafref*)type)->realtype = typeiter;
Radek Krejcicdfecd92018-11-26 11:27:32 +01007312 } else if (type->basetype == LY_TYPE_UNION) {
7313 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
7314 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
7315 /* store pointer to the real type */
7316 for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype;
7317 typeiter->basetype == LY_TYPE_LEAFREF;
7318 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
7319 ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter;
7320 }
7321 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01007322 }
7323 }
7324 }
Radek Krejciec4da802019-05-02 13:02:41 +02007325
Radek Krejci474f9b82019-07-24 11:36:37 +02007326 /* finish incomplete default values compilation */
7327 for (u = 0; u < ctx.dflts.count; ++u) {
7328 struct ly_err_item *err = NULL;
7329 struct lysc_incomplete_dflt *r = ctx.dflts.objs[u];
Radek Krejci950f6a52019-09-12 17:15:32 +02007330 ret = r->dflt->realtype->plugin->store(ctx.ctx, r->dflt->realtype, r->dflt->original, strlen(r->dflt->original),
Radek Krejci474f9b82019-07-24 11:36:37 +02007331 LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE | LY_TYPE_OPTS_SECOND_CALL, lys_resolve_prefix,
7332 (void*)r->dflt_mod, LYD_XML, r->context_node, NULL, r->dflt, NULL, &err);
7333 if (err) {
7334 ly_err_print(err);
7335 ctx.path[0] = '\0';
Michal Vasko03ff5a72019-09-11 13:49:33 +02007336 lysc_path(r->context_node, LYSC_PATH_LOG, ctx.path, LYSC_CTX_BUFSIZE);
Radek Krejci474f9b82019-07-24 11:36:37 +02007337 LOGVAL(ctx.ctx, LY_VLOG_STR, ctx.path, LYVE_SEMANTICS,
7338 "Invalid default - value does not fit the type (%s).", err->msg);
7339 ly_err_free(err);
7340 }
7341 LY_CHECK_GOTO(ret, error);
7342 }
7343
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007344 /* validate non-instantiated groupings from the parsed schema,
7345 * without it we would accept even the schemas with invalid grouping specification */
7346 ctx.options |= LYSC_OPT_GROUPING;
7347 LY_ARRAY_FOR(sp->groupings, u) {
7348 if (!(sp->groupings[u].flags & LYS_USED_GRP)) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007349 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, node_p, &sp->groupings[u]), error);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007350 }
7351 }
7352 LY_LIST_FOR(sp->data, node_p) {
7353 grps = (struct lysp_grp*)lysp_node_groupings(node_p);
7354 LY_ARRAY_FOR(grps, u) {
7355 if (!(grps[u].flags & LYS_USED_GRP)) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007356 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, node_p, &grps[u]), error);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007357 }
7358 }
7359 }
7360
Radek Krejci474f9b82019-07-24 11:36:37 +02007361 if (ctx.ctx->flags & LY_CTX_CHANGED_TREE) {
7362 /* TODO Deviation has changed tree of a module(s) in the context (by deviate-not-supported), it is necessary to recompile
7363 leafref paths, default values and must/when expressions in all schemas of the context to check that they are still valid */
7364 }
7365
Radek Krejci1c0c3442019-07-23 16:08:47 +02007366 ly_set_erase(&ctx.dflts, free);
Radek Krejcia3045382018-11-22 14:30:31 +01007367 ly_set_erase(&ctx.unres, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01007368 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02007369 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01007370 LY_ARRAY_FREE(augments);
Radek Krejcia3045382018-11-22 14:30:31 +01007371
Radek Krejciec4da802019-05-02 13:02:41 +02007372 if (ctx.options & LYSC_OPT_FREE_SP) {
Radek Krejci19a96102018-11-15 13:38:09 +01007373 lysp_module_free(mod->parsed);
7374 ((struct lys_module*)mod)->parsed = NULL;
7375 }
7376
Radek Krejciec4da802019-05-02 13:02:41 +02007377 if (!(ctx.options & LYSC_OPT_INTERNAL)) {
Radek Krejci95710c92019-02-11 15:49:55 +01007378 /* remove flag of the modules implemented by dependency */
7379 for (u = 0; u < ctx.ctx->list.count; ++u) {
7380 m = ctx.ctx->list.objs[u];
7381 if (m->implemented == 2) {
7382 m->implemented = 1;
7383 }
7384 }
7385 }
7386
Radek Krejci19a96102018-11-15 13:38:09 +01007387 ((struct lys_module*)mod)->compiled = mod_c;
7388 return LY_SUCCESS;
7389
7390error:
Radek Krejci95710c92019-02-11 15:49:55 +01007391 lys_feature_precompile_revert(&ctx, mod);
Radek Krejci1c0c3442019-07-23 16:08:47 +02007392 ly_set_erase(&ctx.dflts, free);
Radek Krejcia3045382018-11-22 14:30:31 +01007393 ly_set_erase(&ctx.unres, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01007394 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02007395 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01007396 LY_ARRAY_FREE(augments);
Radek Krejci19a96102018-11-15 13:38:09 +01007397 lysc_module_free(mod_c, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01007398 mod->compiled = NULL;
7399
7400 /* revert compilation of modules implemented by dependency */
7401 for (u = 0; u < ctx.ctx->list.count; ++u) {
7402 m = ctx.ctx->list.objs[u];
7403 if (m->implemented == 2) {
7404 /* revert features list to the precompiled state */
7405 lys_feature_precompile_revert(&ctx, m);
7406 /* mark module as imported-only / not-implemented */
7407 m->implemented = 0;
7408 /* free the compiled version of the module */
7409 lysc_module_free(m->compiled, NULL);
7410 m->compiled = NULL;
7411 }
7412 }
7413
Radek Krejci19a96102018-11-15 13:38:09 +01007414 return ret;
7415}